SIP Phone Log 7

xiaoxiao2021-03-06  59

SIP Message Analysis Design

Because SIP's protocol is complex, it needs to break by, which we started grammar analysis of its protocol.

1) SIP includes two messages: generation and processing

a) Generating requests include requesting a message header space message body

b) Processing request includes status line message header space message body

2) Requesting line includes Method Request-URI SIP-VERSION

a) Method includes:

l invite

l apk

l Cancel

lregiste

l Bye

l Options

3) Status line includes SIP-VERSION STATUS-CODE REASON-Phrase

4) The message header includes multiple messages

5) Message head type

a) Vias

b) froms

c) TOS

d) Call_ids

e) CSEQS

f) Max_forwards

g) Contacts

h) Content_Types

I) Content_lengths

j) supported

K) Require

We have just started to analyze in simple messages: Invate Sip: bob.johnson@company.com Sip / 2.0VIA: SIP / 2.0 / UDP Workstation1000.UNiversity.com:5060From: Laura Brown To: Bob Johnson Call-ID: 12345678@workstation1000.university.comCSeq: 1 INVATEContact: Laura Brown Content-Type: application / SDPContent-Length: 154 Mustable Data Types in SIP Messages: Plastic, Flooring, String, Email, Domain Name, Random Strings, Block, Space, etc ... Let's write the corresponding regular expression first:

1. WS [/ t] // space

2. NL / N

3. Whitespace ^ [/ t] * / n // blank line

4. URL ([^ / T / ] *) @ ([^ / T / ] *)

5. qstring / "[^ /" / n] * [/ "/ n] // matches" DFDFD "

6. Commpro UDP | TCP | TLS | SCTP // Communication Protocol

7. Float [0-9] *. [0-9] *

8. Integer [0-9]

9. Threenum [0-9] {3} // Triple Integer

10. String [A-ZA-Z] [A-ZA-Z0-9]

11. Field [A-ZA-Z0-9.]

12. Name [A-ZA-Z] [] * [A-ZA-Z] *

13. Localid [A-ZA-Z0-9 -] @ [A-ZA-ZA-Z0-9.] These expressions are relatively simple, and readers can refer to the regular expression control.

Here first, the friends developed by Lex and Yacc, WINDOW may be a bit unfamiliar, but it is very popular in xnix .lex is the lexical identification tool, YACC is a syntax recognition tool. Complete a computer language compiler If you don't introduce, you can refer to: l www-900.ibm.com/developerWorks/ CN / Linux / SDK / LEX / INDEX.SHTML

l http://dinosaur.compilertools.net/

l "Lex and YACC (Second Edition)"

SIP message can be quickly resolved by the use of these tools. Lex and yacc, and because the C code can be generated, we can quickly migrate to our program code.

First, we write lex: ******************************************** *************% {# incrude

Extern int Lineno;

%} WS [/ t]

NL / N

Whitespace ^ [/ t] * / n

URL ([^ / T / ] *) @ ([^ / T / ] *)

Qstring / "[^ /" / n] * [/ "/ n]

CommPro UDP | TCP | TLS | SCTP

Float [0-9] *. [0-9] *

INTEGER [0-9]

Threenum [0-9] {3}

STR [A-ZA-Z] [A-ZA-Z0-9]

Field [A-ZA-Z0-9.]

Name [A-ZA-Z] [] * [A-ZA-Z] *

Localid [A-ZA-ZA-Z0-9 -] @ [A-ZA-Z0-9.]

%% {ws};

{whitespace} {return white;}

{Url} {yylval.string = strdup (yytext); return URL;}

{qstring} {yylval.string = strdup (YYTEXT 1); / * Skip open quote * / if (YYLVAL.STRING [YYLENG-2]! = '") Warning (" Unterminated Character String ", (char *) 0); else yylval.string [yYLENG-2] = '/ 0'; / * remove close quote * / return qstring;}

{Commpro} {yylval.string = strdup (yytext); return COMMPRO;}

{Float} {yylval.floatval = atof (yytext); return FLOAT;}

{Integer} {yylval.intval = atoi (yytext); return INTEGER;}

{Threenum} {yylval.intval = atoi (yytext); return THREENUM;}

{Str} {yylval.string = strdup (yytext); return STR;}

{Field} {yylval.string = strdup (yytext); return FIELD;}

{Name} {yylval.string = strdup (yytext); return NAME;}

{Localid} {yylval.string = strdup (yytext); return LOCALID;} / * command * / INVITE {return INVITE;} ACK {return ACK;} CANCEL {return CANCEL;} REGISTER {return REGISTER;} BYE {return BYE Options {return Options;} / * msg head * / via: {return.com;} to: {return;} call-id: {return callid;} cseq: {return; } Max-Forwards: {return MAXFORWARDS;} Contact: {return CONTACT;} Content_type: {return cONTACTTYPE;} Content_length: {return CONTACTLENGTH;} Supported: {return SUPPORTED;} Require: {return REQUIRE;}

{nl} {lineno ;}. {return yytext [0];} %% **************************************** ************************************* After writing Lex, we will come forward the front. Syntax analysis becomes YACC code: *************************************************** ******************% {# include #include #include %}

% union {char * String; / * string buffer * / int tent; double floatval; int cmd; / * command value * / int headfield;}

% Token QSTRING LOCALID COMMPRO URL FIELD NAME STR% token INVITE ACK CANCEL REGISTER BYE OPTIONS% token VIA FROM TO CALLID CSEQ MAXFORWARD CONTACT CONTACTTYP CONTACTLEN SUPPORTED REQUIRE% token FLOAT% token Integer threnum

% Type Lineno %%

START: SIP_MSG; / * SIP includes two messages: generating and processing * / SIP_MSG: generating_request | sending_request; / ******************************************************************************************************************************************************************************************************* *************************************** * request includes requesting bank header blank message body * / generating_request: Req_line msg_head spaceline msg_body | start_line msg_heads;

/ * Processing includes status line message header space message * / sending_request: status_line msg_head spaceLine msg_body / ************************************************** ********************************* *************** ********************************************************* / / * Request line includes Method Request-URI SIP-VERSION * / REQ_LINE: Command Require_URL SIP_VER;

/ * Status line includes SIP-VERSION STATUS-CODE REASON-phrase * / status_line: SIP_VER STATUS_CODE REASON_PHRASE; / **************************************** ******************************************* /

/ ************************************************** ************** / / * Messaging includes multiple messages * / msg_heads: msg_head | msg_heads msg_head; / * can be multiple as: msg_heads: via from To or msg_head: via from To CSEQ CALL_ID * /

// msg_head: vias froms TOS CALL_IDS CSEQS MAX_FORWARDS Contacts Content_Types Content_Length

/ * SD type * / msg_head: vias / * any * / | foms | TOS | CALL_IDS | CSEQS | MAX_FORWARDS | Contacts | Content_types | Content_lengths | Supported | Require; / *********** *********************************************************** * /

/ ************************************************** ****************** /

VIAS: VIA / * One or more * / | vias via; via: via sip_ver_pro field; foms: from name, * one * /; from name request_url; TOS: to / * one * /; to: to name required_url Call_ids: Call_ID / * A * /; Call_ID: Call_ID URL; CSEQS: CSEQ / * A * /; CSEQ: CSEQ Integer Command; Max_forwards: max_forward / * A * /; Max_forward: maxforwards integer;

Contacts: / * empty or one * / | contact; contact: contact name required_url; content_types: / * empty or one * / | content_type; content_type: | Media_Type;

Content_lengths: / * empty or one * / | Content_length, "/ ***************************************************** ***************************************** /

/ ************************************************** ****************** / COMMAND: INVITE | ACK | Cancel | Register | BYE | Options; Status_Code: threenum

Reason_phrase: STR;

Require_url: SIP ":" URL {Printf ("% s", $ 2);} | "<" SIP URL ">" SIP_VER: "SIP /" Integer {Printf ("% D", $ 2);

SIP: SIP | SIPS;

SIP_VER_PRO: SIP_VER "/" common; field: field ":" Integer; Media-type: STR "/" str; / ********************** *************************************************** /

Makefile:

*****************************

CC = GCC

LIBS = -ly -ll -lm

Lex = flex

Yacc = yacc

Cflags = -dyydebug = 1

SIPANALYSE: Y. Tab.o Lex.Yy.o

$ (Cc) $ (cflags) -o sipanalyse y.tab.o lex.yy.o $ (libs)

Lex.yy.o: lex.yy.c y.tab.h

Y. Tab.c y.tab.h: SIP.Y

$ (YACC) -d Sip.y

Lex.yy.c: Sip.l

$ (Lex) sip.l

************************************************

The above code has not been checked. If you have any questions, please reply, and your interest will be modified on this basis.

Big dog

Yirui1@21cn.com

转载请注明原文地址:https://www.9cbs.com/read-86612.html

New Post(0)