Diameter消息解析工具
1 #ifndef PUBLIC_H_INCLUDED
2 #define PUBLIC_H_INCLUDED
3
4 #include <stdio.h>
5 #include < string.h>
6
7
8 #ifdef _DEBUG_
9
10 #define debug(
) fprintf(stderr, __VA_ARGS__)
11 #define ASSERT(x ,
) do { \
12 if( !( x ) ) \
13 { \
14 fprintf(stderr, "LINE:%d,FUNC:%s ,TIME:%s\n"\
15 , __LINE__, __func__, __TIME__); \
16 fprintf( stderr, __VA_ARGS__); \
17 exit( 1 ); \
18 } \
19 } while ( 0 )
20 #else
21 #define debug(
)
22 #define ASSERT(x ,
)
23 #endif
24
25
26
27
28 unsigned int ascii_char_maps[ 256 ] ;
29
30
31
32 #endif
33
#include "public.h"
2 #define PUBLIC_H_INCLUDED
3
4 #include <stdio.h>
5 #include < string.h>
6
7
8 #ifdef _DEBUG_
9
10 #define debug(

11 #define ASSERT(x ,

12 if( !( x ) ) \
13 { \
14 fprintf(stderr, "LINE:%d,FUNC:%s ,TIME:%s\n"\
15 , __LINE__, __func__, __TIME__); \
16 fprintf( stderr, __VA_ARGS__); \
17 exit( 1 ); \
18 } \
19 } while ( 0 )
20 #else
21 #define debug(

22 #define ASSERT(x ,

23 #endif
24
25
26
27
28 unsigned int ascii_char_maps[ 256 ] ;
29
30
31
32 #endif
33
void init_ascii_char_maps()
{
int i ;
memset( ascii_char_maps , -1, sizeof ascii_char_maps);
for( i = 'a' ; i < 'g' ; i++)
{
ascii_char_maps[ i ] = i - 'a' + 0x0a;
}
for( i = 'A' ; i < 'G' ; i++)
{
ascii_char_maps[ i ] = i - 'A' + 0x0a;
}
for( i = '0' ; i <= '9' ; i++ )
{
ascii_char_maps[ i ] = i - '0' ;
}
}
/* *
* "A0" ==> 0xA0
* 两个字节数据 --> 一个字节数值
*/
int __HexStrToHexInt( const char *szSrc , unsigned char *chDst )
{
unsigned int tmp_val;
*chDst = 0;
tmp_val = ascii_char_maps [ szSrc[0] ] ;
ASSERT( tmp_val != -1 , "unknown character value[%x]\n" , szSrc[0]);
*chDst = (tmp_val << 4);
tmp_val = ascii_char_maps [ szSrc[1] ] ;
ASSERT( tmp_val != -1 , "unknown character value[%x]\n" , szSrc[1]);
*chDst += tmp_val;
debug("[%c,%c]==>[%02X]\n",szSrc[0],szSrc[1], *chDst);
return 0;
}
/* *
* "A0B0C0" ==> 0xA0B0C0
* 2n个字节数据 --> n个字节数值(n >= 0)
*/
int HexStrToHexInt( const char *szSrc , unsigned char *chDst , int *length )
{
int i,j,ret ;
int src_len = strlen(szSrc);
const char *pSrc;
char *pDst;
if( src_len % 2 != 0 )
{
fprintf( stderr, "szSrc length[%d] error! \n", src_len);
return -1;
}
if( src_len == 0 )
{
chDst[0] = 0;
*length = 0;
return 0;
}
for( pSrc = szSrc, pDst = chDst ; (pSrc - szSrc) < src_len ; pSrc += 2, pDst++ )
{
ret = __HexStrToHexInt( pSrc , pDst );
if( ret < 0 )
{
fprintf( stderr, "__HexStrToHexInt return [%d] error\n", ret );
return -2; /* 内部字符串格式错误,存在非法字符 */
}
}
*length = src_len/2;
chDst[*length] = 0;
return 0;
}
1 #include "public.h"
2
3 /* the attribute struct of message head */
4 typedef struct _dcca_msg_head_cmd_t
5 {
6 unsigned int r :1; /* *< Request */
7 unsigned int p :1; /* *< Proxiable */
8 unsigned int e :1; /* *< Error */
9 unsigned int t :1; /* *< Potentially re-transmitted */
10 unsigned int rsvd :4; /* *< Reserved */
11 unsigned int uiCmdCode:24; /* Command-Code */
12 } DCCA_MSG_CMD_T;
13
14
15 /* the struct of message head */
16
17 typedef struct _dcca_msg_head_t{
18 unsigned char ucVer; /* Version */
19 unsigned int uiLen:24; /* Message Length */
20 DCCA_MSG_CMD_T command; /* command flags */
21 unsigned int uiAppID; /* Application-ID */
22 unsigned int uiHbh; /* Hop-by-Hop Identifier */
23 unsigned int uiEte; /* End-to-End Identifier */
24 } DCCA_MSG_HEAD_T ;
25
26
27 void display_msg_head( DCCA_MSG_HEAD_T *p_msg_head )
28 {
29 DCCA_MSG_HEAD_T *p;
30 p = p_msg_head;
31 fprintf(stderr, "ver:%d,msg_len:%x\nflag{r:%d,p:%d,e:%d,t:%d,rsvd:%d},command_code:%d\n",
32 p->ucVer,p->uiLen,p->command.r,p->command.p,p->command.e, p->command.t, p->command.rsvd, p->command.uiCmdCode);
33 }
34
35 #define DCCA_MSG_HEAD_LEN sizeof( DCCA_MSG_HEAD_T )
36
37 /* the attribute struct of message head */
38 typedef struct _dcca_avp_node_flag_t
39 {
40 unsigned int v :1; /* *< Vender-ID flag */
41 unsigned int m :1; /* *< Must 指明对该AVP的支持是否是必需的 */
42 unsigned int p :1; /* *< 指明为保证端到端安全需要加密 */
43 unsigned int rsvd :5; /* *< Reserved */
44 } DCCA_AVP_FLAG_T;
45
46
47
48 typedef struct _dcca_avp_node_head_t {
49 unsigned int avp_code; /* AVP Code */
50 DCCA_AVP_FLAG_T flag ; /* V M P r r r r r */
51 unsigned int avp_length; /* AVP Length */
52 unsigned int verdor_id; /* 制造商ID */
53 } DCCA_AVP_CODE_HEAD_T;
54
55 #define DCCA_AVP_CODE_HEAD_LEN sizeof( DCCA_AVP_CODE_HEAD_T )
56
57 int main( int argc , char *argv[] )
58 {
59
60 int i,len;
61
62 char *szSrc = "0100035CC000011000000004080004C5F280C82B000001074000003443434747534E3031424857312E636D63632E636F6D3B333631323539303636323B32323635363B3239393137000001024000000C00000004000001084000001D43434747534E3031424857312E636D63632E636F6D0000000000012840000010636D63632E636F6D0000011B40000010636D63632E636F6D00000125400000166A696C696E2E636D63632E636F6D0000000001A04000000C000000020000019F4000000C00000003000001CD400000156770727340636D63632E636F6D000000000001164000000CD63F4A6E000000374000000CD753C9CA000001BB4000002C000001C24000000C00000000000001BC4000001538363133373034343439353836000000000001BB4000002C000001C24000000C00000001000001BC4000001734363030303434323830393536313200000001C74000000C00000001000001C840000064000001B540000008000001BE40000038000001A54000001000000000000001880000019C4000001000000000000000D00000019E4000001000000000000000B8000001B04000000C7735940900000368C0000010000028AF00000004000001CA00000024000001CB0000000C00000000000001CC00000010686992005292918700000369C000017C000028AF0000036AC0000170000028AF00000002C0000010000028AF17346C9B00000003C0000010000028AF00000000000004CBC0000012000028AF00010AE4D18A000000000005C0000027000028AF30352D32333932316639313936393739373734666239373937303000000004CCC0000012000028AF0001DDB1CC0D00000000034FC0000012000028AF0001DDB1CC3100000000034EC0000012000028AF0001C0A82090000000000008C0000011000028AF343630303000000000000009C0000011000028AF34363030300000000000000AC000000D000028AF350000000000001E4000000D636D6E65740000000000000CC000000D000028AF300000000000000DC0000010000028AF3034303000000012C0000011000028AF343630303000000000000017C000000E000028AF23000000000003ECC000001A000028AF755F705F636D63635F636D6E6574000000000016C0000014000028AF0064F00043E780F300000015C000000D000028AF02000000";
63
64 char szDst[ 4096 ];
65
66 DCCA_MSG_HEAD_T dcca_msg_head;
67
68 init_ascii_char_maps();
69 memset( &dcca_msg_head, 0, sizeof ( DCCA_MSG_HEAD_T ));
70 len = 0;
71 memset( szDst, 0, sizeof( szDst ) );
72 FILE *fp = fopen( "./a.txt" , "wb+");
73 for( i = 0 ;i < 2; i++)
74 {
75 HexStrToHexInt(szSrc, szDst,&len);
76 /**/
77 fwrite( szDst, len , 1 ,fp );
78 /**/
79
80 memcpy( &dcca_msg_head, szDst, DCCA_MSG_HEAD_LEN );
81 display_msg_head( &dcca_msg_head );
82 }
83 fclose(fp);
84
85 return 0;
86 }
87