parser seems to work
[sip-router] / test.c
1 /*
2  * tst  
3  */
4
5 #include <stdio.h>
6 #include <string.h>
7
8 #include "msg_parser.h"
9 #include "dprint.h"
10
11 #define BSIZE 1024
12 char buf[BSIZE+1];
13
14 void main()
15 {
16         char* rest;
17         char* tmp;
18         char* first_via;
19         char* second_via;
20         struct msg_start fl;
21         struct hdr_field hf;
22         struct via_body vb1, vb2;
23
24         int len;
25         int offset;
26         int r;
27
28         while(!feof(stdin)){
29                 len=fread(buf,1,BSIZE,stdin);
30                 buf[len+1]=0;
31                 printf("read <%s>(%d)\n",buf,strlen(buf));
32                 fflush(stdin);
33         
34                 /* eat crlf from the beginning */
35                 for (tmp=buf; (*tmp=='\n' || *tmp=='\r')&&
36                                 tmp-buf < len ; tmp++);
37                 offset=tmp-buf;
38                 rest=parse_first_line(tmp, len-offset, &fl);
39                 offset+=rest-tmp;
40                 tmp=rest;
41                 switch(fl.type){
42                         case SIP_INVALID:
43                                 printf("invalid message\n");
44                                 break;
45                         case SIP_REQUEST:
46                                 printf("SIP Request:\n");
47                                 printf(" method:  <%s>\n",fl.u.request.method);
48                                 printf(" uri:     <%s>\n",fl.u.request.uri);
49                                 printf(" version: <%s>\n",fl.u.request.version);
50                                 break;
51                         case SIP_REPLY:
52                                 printf("SIP Reply  (status):\n");
53                                 printf(" version: <%s>\n",fl.u.reply.version);
54                                 printf(" status:  <%s>\n",fl.u.reply.status);
55                                 printf(" reason:  <%s>\n",fl.u.reply.reason);
56                                 break;
57                         default:
58                                 printf("unknown type %d\n",fl.type);
59                 }
60                 
61                 /*find first Via: */
62                 hf.type=HDR_ERROR;
63                 first_via=0;
64                 second_via=0;
65                 do{
66                         rest=get_hdr_field(tmp, len-offset, &hf);
67                         offset+=rest-tmp;
68                         tmp=rest;
69                         switch (hf.type){
70                                 case HDR_ERROR:
71                                         DPrint("ERROR: bad header  field\n");
72                                         goto  error;
73                                 case HDR_EOH: 
74                                         goto eoh;
75                                 case HDR_VIA:
76                                         if (first_via==0) first_via=hf.body;
77                                         else if (second_via==0) second_via=hf.body;
78                                         break;
79                         }
80                         printf("header field type %d, name=<%s>, body=<%s>\n",
81                                 hf.type, hf.name, hf.body);
82
83                 
84                 }while(hf.type!=HDR_EOH && rest-buf < len);
85
86         eoh:
87                 /* replace cr/lf with space in first via */
88                 for (tmp=first_via;(first_via) && (*tmp);tmp++)
89                         if ((*tmp=='\r')||(*tmp=='\n')) *tmp=' ';
90
91                 printf("first via: <%s>\n", first_via);
92                 tmp=parse_via_body(first_via, strlen(first_via), &vb1);
93                 if (vb1.error!=VIA_PARSE_OK){
94                         DPrint("ERROR: parsing via body: %s\n", first_via);
95                         goto error;
96                 }
97                 /* compact via */
98                 if (vb1.next) second_via=vb1.next;
99                 if (second_via) {
100                         tmp=parse_via_body(second_via, strlen(second_via), &vb2);
101                         if (vb2.error!=VIA_PARSE_OK){
102                                 DPrint("ERROR: parsing via body: %s\n", second_via);
103                                 goto error;
104                         }
105                 }
106                 
107                 /* dump parsed data */
108                 printf(" first  via: <%s/%s/%s> <%s:%d>",
109                                 vb1.name, vb1.version, vb1.transport, vb1.host, vb1.port);
110                 if (vb1.params) printf(";<%s>", vb1.params);
111                 if (vb1.comment) printf(" <%s>", vb1.comment);
112                 printf ("\n");
113                 if (second_via){
114                         printf(" second via: <%s/%s/%s> <%s:%d>",
115                                         vb2.name, vb2.version, vb2.transport, vb2.host, vb2.port);
116                         if (vb2.params) printf(";<%s>", vb2.params);
117                         if (vb2.comment) printf(" <%s>", vb2.comment);
118                         printf ("\n");
119                 }
120
121                 
122
123         error:
124                 /* find endof msg */
125                 printf("rest:(buffer=%x, rest=%x)\n%s\n.\n",buf,rest,rest);
126
127                 for (r=0; r<len+1;r++)
128                         printf("%02x ", buf[r]);
129                 printf("\n*rest=%02x\n",*rest);
130                 
131         }
132 }
133                                 
134