Browse code

parse_to() - accept also \0 as EOH; don't write anymore 0 into the header during parsing. parse_from_header() - rebuilded according with the new parse_to()

Bogdan-Andrei Iancu authored on 03/12/2002 11:45:52
Showing 3 changed files
... ...
@@ -1,5 +1,4 @@
1 1
 /*
2
- * @author Stelios Sidiroglou-Douskos <ssi@fokus.gmd.de>
3 2
  * $Id$
4 3
  *
5 4
  * Copyright (C) 2001-2003 Fhg Fokus
... ...
@@ -41,39 +40,44 @@
41 40
  * anything in core that is not *needed* so this method gets called by 
42 41
  * rad_acc module and any other modules that needs the FROM header.
43 42
  *
44
- * params: hdr : Hook to the from header
43
+ * params: msg : sip msg
45 44
  * returns 0 on success,
46
- *		   -1 on failure.
45
+ *        -1 on failure.
47 46
  */
48
-int parse_from_header(struct hdr_field* hdr) 
47
+int parse_from_header( struct sip_msg *msg)
49 48
 {
50 49
 	struct to_body* from_b;
51
-	
50
+
51
+	if ( !msg->from && ( parse_headers(msg,HDR_FROM,0)==-1 || !msg->from)) {
52
+		LOG(L_ERR,"ERROR:parse_from_header: bad msg or missing FROM header\n");
53
+		goto error;
54
+	}
55
+
56
+	/* maybe the header is already parsed! */
57
+	if (msg->from->parsed)
58
+		return 0;
59
+
60
+	/* bad luck! :-( - we have to parse it */
61
+	/* first, get some memory */
52 62
 	from_b = pkg_malloc(sizeof(struct to_body));
53 63
 	if (from_b == 0) {
54
-		LOG(L_ERR, "parse_from_header: out of memory\n");
64
+		LOG(L_ERR, "ERROR:parse_from_header: out of pkg_memory\n");
55 65
 		goto error;
56 66
 	}
57
-			
67
+
68
+	/* now parse it!! */
58 69
 	memset(from_b, 0, sizeof(struct to_body));
59
-	parse_to(hdr->body.s, hdr->body.s + hdr->body.len + 1, from_b);
70
+	parse_to(msg->from->body.s,msg->from->body.s+msg->from->body.len+1,from_b);
60 71
 	if (from_b->error == PARSE_ERROR) {
61
-		LOG(L_ERR, "ERROR: parse_from_header: bad from header\n");
72
+		LOG(L_ERR, "ERROR:parse_from_header: bad from header\n");
62 73
 		pkg_free(from_b);
63 74
 		goto error;
64 75
 	}
65
-	hdr->parsed = from_b;	
66
-	DBG("DEBUG: parse_from_header: <%s> [%d]; uri=[%.*s] \n",
67
-		hdr->name.s, hdr->body.len, from_b->uri.len, from_b->uri.s);
68
-	DBG("DEBUG: from body [%.*s]\n",from_b->body.len, from_b->body.s);	
76
+	msg->from->parsed = from_b;
69 77
 
70 78
 	return 0;
71
-
72
-	error:
73
-	/* more debugging, msg->orig is/should be null terminated*/
74
-	LOG(L_ERR, "ERROR: parse_from_header: \n");
79
+error:
75 80
 	return -1;
76
-
77 81
 }
78 82
 
79 83
 
... ...
@@ -26,15 +26,14 @@
26 26
  */
27 27
 
28 28
 
29
-#ifndef PARSE_FROM
30
-#define PARSE_FROM
29
+#ifndef _PARSE_FROM_H
30
+#define _PARSE_FROM_H
31
+
32
+#include "msg_parser.h"
31 33
 
32
-#include "../str.h"
33
-#include "parse_to.h"
34
-#include "hf.h"
35 34
 /*
36 35
  * To header field parser
37 36
  */
38
-int parse_from_header(struct hdr_field* hdr);
37
+int parse_from_header( struct sip_msg *msg);
39 38
 
40 39
 #endif
... ...
@@ -39,16 +39,16 @@ enum {
39 39
 };
40 40
 
41 41
 
42
-enum { 
43
-	START_TO, DISPLAY_QUOTED, E_DISPLAY_QUOTED, DISPLAY_TOKEN, 
44
-	S_URI_ENCLOSED, URI_ENCLOSED, E_URI_ENCLOSED, 
42
+enum {
43
+	START_TO, DISPLAY_QUOTED, E_DISPLAY_QUOTED, DISPLAY_TOKEN,
44
+	S_URI_ENCLOSED, URI_ENCLOSED, E_URI_ENCLOSED,
45 45
 	URI_OR_TOKEN, MAYBE_URI_END, END, F_CR, F_LF, F_CRLF
46 46
 };
47 47
 
48 48
 
49
-enum { 
50
-	S_PARA_NAME=20, PARA_NAME, S_EQUAL, S_PARA_VALUE, TAG1, TAG2, 
51
-	TAG3, PARA_VALUE_TOKEN , PARA_VALUE_QUOTED, E_PARA_VALUE, PARA_START
49
+enum {
50
+	S_PARA_NAME=20, PARA_NAME, S_EQUAL, S_PARA_VALUE, TAG1, TAG2,
51
+	TAG3, PARA_VALUE_TOKEN , PARA_VALUE_QUOTED, E_PARA_VALUE
52 52
 };
53 53
 
54 54
 
... ...
@@ -78,9 +78,8 @@ static /*inline*/ char* parse_to_param(char *buffer, char *end,
78 78
 	char  *tmp;
79 79
 
80 80
 	param=0;
81
-	status=PARA_START;
82
-	saved_status=PARA_START;
83
-
81
+	status=E_PARA_VALUE;
82
+	saved_status=E_PARA_VALUE;
84 83
 	for( tmp=buffer; tmp<end; tmp++)
85 84
 	{
86 85
 		switch(*tmp)
... ...
@@ -95,12 +94,10 @@ static /*inline*/ char* parse_to_param(char *buffer, char *end,
95 94
 					case TAG1:
96 95
 					case TAG2:
97 96
 						param->name.len = tmp-param->name.s;
98
-						*tmp=0;
99 97
 						status = S_EQUAL;
100 98
 						break;
101 99
 					case PARA_VALUE_TOKEN:
102 100
 						param->value.len = tmp-param->value.s;
103
-						*tmp=0;
104 101
 						status = E_PARA_VALUE;
105 102
 						add_param( param , to_b );
106 103
 						break;
... ...
@@ -128,13 +125,11 @@ static /*inline*/ char* parse_to_param(char *buffer, char *end,
128 125
 					case TAG1:
129 126
 					case TAG2:
130 127
 						param->name.len = tmp-param->name.s;
131
-						*tmp=0;
132 128
 						saved_status = S_EQUAL;
133 129
 						status = F_LF;
134 130
 						break;
135 131
 					case PARA_VALUE_TOKEN:
136 132
 						param->value.len = tmp-param->value.s;
137
-						*tmp=0;
138 133
 						saved_status = E_PARA_VALUE;
139 134
 						status = F_LF;
140 135
 						add_param( param , to_b );
... ...
@@ -168,13 +163,11 @@ static /*inline*/ char* parse_to_param(char *buffer, char *end,
168 163
 					case TAG1:
169 164
 					case TAG2:
170 165
 						param->name.len = tmp-param->name.s;
171
-						*tmp=0;
172 166
 						saved_status = S_EQUAL;
173 167
 						status = F_CR;
174 168
 						break;
175 169
 					case PARA_VALUE_TOKEN:
176 170
 						param->value.len = tmp-param->value.s;
177
-						*tmp=0;
178 171
 						saved_status = E_PARA_VALUE;
179 172
 						status = F_CR;
180 173
 						add_param( param , to_b );
... ...
@@ -191,6 +184,34 @@ static /*inline*/ char* parse_to_param(char *buffer, char *end,
191 184
 						goto error;
192 185
 				}
193 186
 				break;
187
+			case 0:
188
+				switch (status)
189
+				{
190
+#ifndef NO_PINGTEL_TAG_HACK
191
+					case TAG3:
192
+						param->type = TAG_PARAM;
193
+						param->name.len = 3;
194
+						status = S_EQUAL;
195
+					case S_EQUAL:
196
+					case S_PARA_VALUE:
197
+						saved_status=status;
198
+						goto endofheader;
199
+#endif
200
+					case PARA_VALUE_TOKEN:
201
+						status = E_PARA_VALUE;
202
+						param->value.len = tmp-param->value.s;
203
+						add_param( param , to_b );
204
+					case E_PARA_VALUE:
205
+						saved_status = status;
206
+						goto endofheader;
207
+						break;
208
+					default:
209
+						LOG( L_ERR , "ERROR: parse_to_param : "
210
+							"unexpected char [%c] in status %d: <<%.*s>> .\n",
211
+							*tmp,status, (int)(tmp-buffer), buffer);
212
+						goto error;
213
+				}
214
+				break;
194 215
 			case '\\':
195 216
 				switch (status)
196 217
 				{
... ...
@@ -219,7 +240,6 @@ static /*inline*/ char* parse_to_param(char *buffer, char *end,
219 240
 						break;
220 241
 					case PARA_VALUE_QUOTED:
221 242
 						param->value.len=tmp-param->value.s-1 ;
222
-						*tmp = 0;
223 243
 						add_param( param , to_b );
224 244
 						status = E_PARA_VALUE;
225 245
 						break;
... ...
@@ -258,8 +278,6 @@ static /*inline*/ char* parse_to_param(char *buffer, char *end,
258 278
 					case PARA_VALUE_TOKEN:
259 279
 						param->value.len=tmp-param->value.s;
260 280
 						add_param(param,to_b);
261
-					case PARA_START:
262
-						*tmp=0;
263 281
 					case E_PARA_VALUE:
264 282
 						param = (struct to_param*)
265 283
 							pkg_malloc(sizeof(struct to_param));
... ...
@@ -398,7 +416,6 @@ static /*inline*/ char* parse_to_param(char *buffer, char *end,
398 416
 					case TAG1:
399 417
 					case TAG2:
400 418
 						param->name.len = tmp-param->name.s;
401
-						*tmp=0;
402 419
 						status = S_PARA_VALUE;
403 420
 						break;
404 421
 					case S_EQUAL:
... ...
@@ -419,6 +436,11 @@ static /*inline*/ char* parse_to_param(char *buffer, char *end,
419 436
 			default:
420 437
 				switch (status)
421 438
 				{
439
+					case TAG1:
440
+					case TAG2:
441
+					case TAG3:
442
+						status = PARA_NAME;
443
+						break;
422 444
 					case PARA_VALUE_TOKEN:
423 445
 					case PARA_NAME:
424 446
 					case PARA_VALUE_QUOTED:
... ...
@@ -459,9 +481,9 @@ endofheader:
459 481
 	return tmp;
460 482
 
461 483
 error:
462
-	LOG(L_ERR, "to_param parse error\n");
463 484
 	if (param) pkg_free(param);
464 485
 	to_b->error=PARSE_ERROR;
486
+	*returned_status = status;
465 487
 	return tmp;
466 488
 }
467 489
 
... ...
@@ -476,7 +498,7 @@ char* parse_to(char* buffer, char *end, struct to_body *to_b)
476 498
 
477 499
 	status=START_TO;
478 500
 	to_b->error=PARSE_OK;
479
-	foo=0;	
501
+	foo=0;
480 502
 
481 503
 	for( tmp=buffer; tmp<end; tmp++)
482 504
 	{
... ...
@@ -494,7 +516,6 @@ char* parse_to(char* buffer, char *end, struct to_body *to_b)
494 516
 						break;
495 517
 					case URI_ENCLOSED:
496 518
 						to_b->uri.len = tmp - to_b->uri.s;
497
-						//*tmp = 0;
498 519
 						status = E_URI_ENCLOSED;
499 520
 						break;
500 521
 					case URI_OR_TOKEN:
... ...
@@ -554,6 +575,22 @@ char* parse_to(char* buffer, char *end, struct to_body *to_b)
554 575
 						goto error;
555 576
 				}
556 577
 				break;
578
+			case 0:
579
+				switch (status)
580
+				{
581
+					case URI_OR_TOKEN:
582
+					case MAYBE_URI_END:
583
+						to_b->uri.len = tmp - to_b->uri.s;
584
+					case END:
585
+						saved_status = status = END;
586
+						goto endofheader;
587
+					default:
588
+						LOG( L_ERR , "ERROR: parse_to : unexpected char [%c] "
589
+							"in status %d: <<%.*s>> .\n",
590
+							*tmp,status, (int)(tmp-buffer), buffer);
591
+						goto error;
592
+				}
593
+				break;
557 594
 			case '\\':
558 595
 				switch (status)
559 596
 				{
... ...
@@ -606,7 +643,6 @@ char* parse_to(char* buffer, char *end, struct to_body *to_b)
606 643
 					case DISPLAY_QUOTED:
607 644
 						break;
608 645
 					case URI_ENCLOSED:
609
-						//*tmp = 0;
610 646
 					case E_URI_ENCLOSED:
611 647
 						to_b->uri.len = tmp - to_b->uri.s;
612 648
 						status = END;
... ...
@@ -659,7 +695,6 @@ char* parse_to(char* buffer, char *end, struct to_body *to_b)
659 695
 					case END:
660 696
 						to_b->body.len = tmp-to_b->body.s;
661 697
 						tmp = parse_to_param(tmp,end,to_b,&saved_status);
662
-						//if (foo) *foo=0;
663 698
 						goto endofheader;
664 699
 					case F_CRLF:
665 700
 					case F_LF:
... ...
@@ -710,12 +745,10 @@ endofheader:
710 745
 	/* check if error*/
711 746
 	switch(status){
712 747
 		case MAYBE_URI_END:
713
-			//*foo=0;
714 748
 			to_b->uri.len = foo - to_b->uri.s;
715 749
 		case END:
716 750
 			to_b->body.len = tmp - to_b->body.s;
717 751
 		case E_PARA_VALUE:
718
-			*(tmp-1)=0;
719 752
 			break;
720 753
 		default:
721 754
 			LOG(L_ERR, "ERROR: parse_to: invalid To -  unexpected "
... ...
@@ -725,7 +758,6 @@ endofheader:
725 758
 	return tmp;
726 759
 
727 760
 error:
728
-	LOG(L_ERR, "to parse error\n");
729 761
 	to_b->error=PARSE_ERROR;
730 762
 	return tmp;
731 763