Browse code

( merged the 0.8.3.4 changes back in 0.8.3.2) - char* pointers replaced by str (char*, len) in the important structs. - old parser is used if OLD_PARSER is defined (default) - new parser is unstable - small cleanups

Andrei Pelinescu-Onciul authored on 15/11/2001 17:44:26
Showing 11 changed files
... ...
@@ -23,7 +23,13 @@ NAME=ser
23 23
 # recommanded: on (speed-up)
24 24
 # STATS allows to print out number of packets processed on CTRL-C; 
25 25
 # implementation still nasty and reports per-process
26
-DEFS=-DNOCR -DMACROEATER -DSTATS #-DNO_DEBUG #-DNO_LOG
26
+# NO_DEBUG turns off some of the debug messages (DBG(...)).
27
+# NO_LOG completely turns of all the logging (and DBG(...))
28
+# DEBUG compiles in some extra debugging code
29
+# OLD_PARSER uses the old and stable parser (from ser 8.3.2)
30
+DEFS=-DNOCR -DMACROEATER -DSTATS -DOLD_PARSER #-DNO_DEBUG #-DNO_LOG
31
+
32
+PROFILE=  # -pg #set this if you want profiling
27 33
 
28 34
 # platform dependent settings
29 35
 
... ...
@@ -32,8 +38,8 @@ ARCH = $(shell uname -s)
32 32
 #common
33 33
 CC=gcc
34 34
 LD=gcc
35
-CFLAGS=-O3 -Wcast-align #-Wmissing-prototypes 
36
-LDFLAGS=-Wl,-O2 -Wl,-E
35
+CFLAGS=-O2 -Wcast-align $(PROFILE)#-Wmissing-prototypes 
36
+LDFLAGS=-Wl,-O2 -Wl,-E $(PROFILE)
37 37
 LEX=flex
38 38
 YACC=bison
39 39
 YACC_FLAGS=-d -b cfg
... ...
@@ -49,7 +55,7 @@ ifeq  ($(ARCH), SunOS)
49 49
 
50 50
 MAKE=gmake
51 51
 YACC=yacc
52
-LDFLAGS=-O2
52
+LDFLAGS=-O2 $(PROFILE)
53 53
 LIBS+=-L/usr/local/lib -lxnet # or -lnsl -lsocket or -lglibc ?
54 54
 
55 55
 endif
... ...
@@ -51,9 +51,14 @@ int do_action(struct action* a, struct sip_msg* msg)
51 51
 		case FORWARD_T:
52 52
 			if (a->p1_type==URIHOST_ST){
53 53
 				/*parse uri*/
54
-				tmp=(msg->new_uri)?msg->new_uri:
55
-						msg->first_line.u.request.uri;
56
-				if (parse_uri(tmp, strlen(tmp), &uri)<0){
54
+				if (msg->new_uri.s){
55
+						tmp=msg->new_uri.s;
56
+						len=msg->new_uri.len;
57
+				}else{
58
+						tmp=msg->first_line.u.request.uri.s;
59
+						len=msg->first_line.u.request.uri.len;
60
+				}
61
+				if (parse_uri(tmp, len, &uri)<0){
57 62
 					LOG(L_ERR, "ERROR: do_action: forward: bad_uri <%s>,"
58 63
 								" dropping packet\n",tmp);
59 64
 					ret=E_UNSPEC;
... ...
@@ -61,8 +66,8 @@ int do_action(struct action* a, struct sip_msg* msg)
61 61
 				}
62 62
 				switch (a->p2_type){
63 63
 					case URIPORT_ST:
64
-									if (uri.port){
65
-										port=strtol(uri.port,&end,10);
64
+									if (uri.port.s){
65
+										port=strtol(uri.port.s,&end,10);
66 66
 										if ((end)&&(*end)){
67 67
 											LOG(L_ERR, "ERROR: do_action: "
68 68
 													"forward: bad port in "
... ...
@@ -83,7 +88,7 @@ int do_action(struct action* a, struct sip_msg* msg)
83 83
 							goto skip;
84 84
 				}
85 85
 				/* create a temporary proxy*/
86
-				p=mk_proxy(uri.host, port);
86
+				p=mk_proxy(uri.host.s, port);
87 87
 				ret=forward_request(msg, p);
88 88
 				free_uri(&uri);
89 89
 				free_proxy(p); /* frees only p content, not p itself */
... ...
@@ -201,23 +206,33 @@ int do_action(struct action* a, struct sip_msg* msg)
201 201
 					break;
202 202
 				}
203 203
 				if (a->type==SET_URI_T){
204
-					if (msg->new_uri) free(msg->new_uri);
204
+					if (msg->new_uri.s) {
205
+							free(msg->new_uri.s);
206
+							msg->new_uri.len=0;
207
+					}
205 208
 					len=strlen(a->p1.string);
206
-					msg->new_uri=malloc(len+1);
207
-					if (msg->new_uri==0){
209
+					msg->new_uri.s=malloc(len+1);
210
+					if (msg->new_uri.s==0){
208 211
 						LOG(L_ERR, "ERROR: do_action: memory allocation"
209 212
 								" failure\n");
210 213
 						ret=E_OUT_OF_MEM;
211 214
 						break;
212 215
 					}
213
-					memcpy(msg->new_uri, a->p1.string, len);
214
-					msg->new_uri[len]=0;
216
+					memcpy(msg->new_uri.s, a->p1.string, len);
217
+					msg->new_uri.s[len]=0;
218
+					msg->new_uri.len=len;
219
+					
215 220
 					ret=1;
216 221
 					break;
217 222
 				}
218
-				if (msg->new_uri) tmp=msg->new_uri;
219
-				else tmp=msg->first_line.u.request.uri;
220
-				if (parse_uri(tmp, strlen(tmp), &uri)<0){
223
+				if (msg->new_uri.s) {
224
+					tmp=msg->new_uri.s;
225
+					len=msg->new_uri.len;
226
+				}else{
227
+					tmp=msg->first_line.u.request.uri.s;
228
+					len=msg->first_line.u.request.uri.len;
229
+				}
230
+				if (parse_uri(tmp, len, &uri)<0){
221 231
 					LOG(L_ERR, "ERROR: do_action: bad uri <%s>, dropping"
222 232
 								" packet\n", tmp);
223 233
 					ret=E_UNSPEC;
... ...
@@ -241,14 +256,14 @@ int do_action(struct action* a, struct sip_msg* msg)
241 241
 				if ((a->type==SET_USER_T)||(a->type==SET_USERPASS_T))
242 242
 					tmp=a->p1.string;
243 243
 				else 
244
-					tmp=uri.user;
244
+					tmp=uri.user.s;
245 245
 				if (tmp){
246 246
 					len=strlen(tmp); if(crt+len>end) goto error_uri;
247 247
 					memcpy(crt,tmp,len);crt+=len;
248 248
 					user=1; /* we have an user field so mark it */
249 249
 				}
250 250
 				if (a->type==SET_USERPASS_T) tmp=0;
251
-				else tmp=uri.passwd;
251
+				else tmp=uri.passwd.s;
252 252
 				/* passwd */
253 253
 				if (tmp){
254 254
 					len=strlen(":"); if(crt+len>end) goto error_uri;
... ...
@@ -264,7 +279,7 @@ int do_action(struct action* a, struct sip_msg* msg)
264 264
 				if ((a->type==SET_HOST_T) ||(a->type==SET_HOSTPORT_T))
265 265
 					tmp=a->p1.string;
266 266
 				else
267
-					tmp=uri.host;
267
+					tmp=uri.host.s;
268 268
 				if (tmp){
269 269
 					len=strlen(tmp); if(crt+len>end) goto error_uri;
270 270
 					memcpy(crt,tmp,len);crt+=len;
... ...
@@ -272,7 +287,7 @@ int do_action(struct action* a, struct sip_msg* msg)
272 272
 				/* port */
273 273
 				if (a->type==SET_HOSTPORT_T) tmp=0;
274 274
 				else if (a->type==SET_PORT_T) tmp=a->p1.string;
275
-				else tmp=uri.port;
275
+				else tmp=uri.port.s;
276 276
 				if (tmp){
277 277
 					len=strlen(":"); if(crt+len>end) goto error_uri;
278 278
 					memcpy(crt,":",len);crt+=len;
... ...
@@ -280,7 +295,7 @@ int do_action(struct action* a, struct sip_msg* msg)
280 280
 					memcpy(crt,tmp,len);crt+=len;
281 281
 				}
282 282
 				/* params */
283
-				tmp=uri.params;
283
+				tmp=uri.params.s;
284 284
 				if (tmp){
285 285
 					len=strlen(";"); if(crt+len>end) goto error_uri;
286 286
 					memcpy(crt,";",len);crt+=len;
... ...
@@ -288,7 +303,7 @@ int do_action(struct action* a, struct sip_msg* msg)
288 288
 					memcpy(crt,tmp,len);crt+=len;
289 289
 				}
290 290
 				/* headers */
291
-				tmp=uri.headers;
291
+				tmp=uri.headers.s;
292 292
 				if (tmp){
293 293
 					len=strlen("?"); if(crt+len>end) goto error_uri;
294 294
 					memcpy(crt,"?",len);crt+=len;
... ...
@@ -297,8 +312,9 @@ int do_action(struct action* a, struct sip_msg* msg)
297 297
 				}
298 298
 				*crt=0; /* null terminate the thing */
299 299
 				/* copy it to the msg */
300
-				if (msg->new_uri) free(msg->new_uri);
301
-				msg->new_uri=new_uri;
300
+				if (msg->new_uri.s) free(msg->new_uri.s);
301
+				msg->new_uri.s=new_uri;
302
+				msg->new_uri.len=crt-new_uri;
302 303
 				free_uri(&uri);
303 304
 				ret=1;
304 305
 				break;
... ...
@@ -191,7 +191,7 @@ int forward_request( struct sip_msg* msg, struct proxy_l * p)
191 191
 		
192 192
 	
193 193
 	/* check if received needs to be added */
194
-	if (check_address(source_ip, msg->via1.host, received_dns)!=0){
194
+	if (check_address(source_ip, msg->via1.host.s, received_dns)!=0){
195 195
 		received_buf=malloc(sizeof(char)*MAX_RECEIVED_SIZE);
196 196
 		if (received_buf==0){
197 197
 			LOG(L_ERR, "ERROR: forward_request: out of memory\n");
... ...
@@ -213,21 +213,22 @@ int forward_request( struct sip_msg* msg, struct proxy_l * p)
213 213
 	/* add via header to the list */
214 214
 	/* try to add it before msg. 1st via */
215 215
 	/*add first via, as an anchor for second via*/
216
-	anchor=anchor_lump(&(msg->add_rm), msg->via1.hdr-buf, 0, HDR_VIA);
216
+	anchor=anchor_lump(&(msg->add_rm), msg->via1.hdr.s-buf, 0, HDR_VIA);
217 217
 	if (anchor==0) goto error;
218 218
 	if (insert_new_lump_before(anchor, line_buf, via_len, HDR_VIA)==0)
219 219
 		goto error;
220 220
 	/* if received needs to be added, add anchor after host and add it */
221 221
 	if (received_len){
222
-		if (msg->via1.params){
223
-				size= msg->via1.params-msg->via1.hdr-1; /*compensate for ';' */
222
+		if (msg->via1.params.s){
223
+				size= msg->via1.params.s-msg->via1.hdr.s-1; /*compensate 
224
+															  for ';' */
224 225
 		}else{
225
-				size= msg->via1.host-msg->via1.hdr+strlen(msg->via1.host);
226
+				size= msg->via1.host.s-msg->via1.hdr.s+msg->via1.host.len;
226 227
 				if (msg->via1.port!=0){
227
-					size+=strlen(msg->via1.hdr+size+1)+1; /* +1 for ':'*/
228
+					size+=strlen(msg->via1.hdr.s+size+1)+1; /* +1 for ':'*/
228 229
 				}
229 230
 		}
230
-		anchor=anchor_lump(&(msg->add_rm), msg->via1.hdr-buf+size, 0, HDR_VIA);
231
+		anchor=anchor_lump(&(msg->add_rm),msg->via1.hdr.s-buf+size,0, HDR_VIA);
231 232
 		if (anchor==0) goto error;
232 233
 		if (insert_new_lump_after(anchor, received_buf, received_len, HDR_VIA) 
233 234
 				==0 ) goto error;
... ...
@@ -291,9 +292,9 @@ int forward_request( struct sip_msg* msg, struct proxy_l * p)
291 291
 	}
292 292
 	
293 293
 	
294
-	if (msg->new_uri){ 
295
-		uri_len=strlen(msg->new_uri); 
296
-		new_len=new_len-strlen(msg->first_line.u.request.uri)+uri_len;
294
+	if (msg->new_uri.s){ 
295
+		uri_len=msg->new_uri.len; 
296
+		new_len=new_len-msg->first_line.u.request.uri.len+uri_len;
297 297
 	}
298 298
 	new_buf=(char*)malloc(new_len+1);
299 299
 	if (new_buf==0){
... ...
@@ -302,16 +303,16 @@ int forward_request( struct sip_msg* msg, struct proxy_l * p)
302 302
 	}
303 303
 
304 304
 	offset=s_offset=0;
305
-	if (msg->new_uri){
305
+	if (msg->new_uri.s){
306 306
 		/* copy message up to uri */
307
-		size=msg->first_line.u.request.uri-buf;
307
+		size=msg->first_line.u.request.uri.s-buf;
308 308
 		memcpy(new_buf, orig, size);
309 309
 		offset+=size;
310 310
 		s_offset+=size;
311 311
 		/* add our uri */
312
-		memcpy(new_buf+offset, msg->new_uri, uri_len);
312
+		memcpy(new_buf+offset, msg->new_uri.s, uri_len);
313 313
 		offset+=uri_len;
314
-		s_offset+=strlen(msg->first_line.u.request.uri); /* skip original uri */
314
+		s_offset+=msg->first_line.u.request.uri.len; /* skip original uri */
315 315
 	}
316 316
 /* copy msg adding/removing lumps */
317 317
 	for (t=msg->add_rm;t;t=t->next){
... ...
@@ -487,23 +488,23 @@ int forward_reply(struct sip_msg* msg)
487 487
 	/*check if first via host = us */
488 488
 	if (check_via){
489 489
 		for (r=0; r<addresses_no; r++)
490
-			if(strcmp(msg->via1.host, names[r])==0) break;
490
+			if(strcmp(msg->via1.host.s, names[r])==0) break;
491 491
 		if (r==addresses_no){
492
-			LOG(L_NOTICE, "ERROR: forward_reply: host in first via!=me : %s\n",
493
-					msg->via1.host);
492
+			LOG(L_NOTICE, "ERROR: forward_reply: host in first via!=me :"
493
+					" %s\n", msg->via1.host);
494 494
 			/* send error msg back? */
495 495
 			goto error;
496 496
 		}
497 497
 	}
498 498
 	/* we must remove the first via */
499 499
 	via_len=msg->via1.size;
500
-	size=msg->via1.hdr-buf;
500
+	size=msg->via1.hdr.s-buf;
501 501
 	DBG("via len: %d, initial size: %d\n", via_len, size);
502 502
 	if (msg->via1.next){
503 503
 		/* keep hdr =substract hdr size +1 (hdr':') and add
504 504
 		 */
505
-		via_len-=strlen(msg->via1.hdr)+1;
506
-		size+=strlen(msg->via1.hdr)+1;
505
+		via_len-=msg->via1.hdr.len+1;
506
+		size+=msg->via1.hdr.len+1;
507 507
 	    DBG(" adjusted via len: %d, initial size: %d\n",
508 508
 				via_len, size);
509 509
 	}
... ...
@@ -525,27 +526,20 @@ int forward_reply(struct sip_msg* msg)
525 525
 			s_offset, offset, 
526 526
 			len-s_offset );
527 527
 	DBG("Sending: to %s:%d, \n%s.\n",
528
-			msg->via2.host, 
528
+			msg->via2.host.s, 
529 529
 			(unsigned short)msg->via2.port,
530 530
 			new_buf);
531 531
 	/* fork? gethostbyname will probably block... */
532
-	he=gethostbyname(msg->via2.host);
532
+	he=gethostbyname(msg->via2.host.s);
533 533
 	if (he==0){
534 534
 		LOG(L_NOTICE, "ERROR:forward_reply:gethostbyname(%s) failure\n",
535
-				msg->via2.host);
535
+				msg->via2.host.s);
536 536
 		goto error;
537 537
 	}
538 538
 	to->sin_family = AF_INET;
539 539
 	to->sin_port = (msg->via2.port)?htons(msg->via2.port):htons(SIP_PORT);
540 540
 	to->sin_addr.s_addr=*((long*)he->h_addr_list[0]);
541 541
 
542
-
543
-
544
-
545
-
546
-
547
-
548
-	
549 542
 #ifdef STATS
550 543
 	stats.total_tx++;
551 544
 #endif
... ...
@@ -34,7 +34,7 @@
34 34
 
35 35
 
36 36
 static char id[]="@(#) $Id$";
37
-static char version[]="ser 0.8.3.2";
37
+static char version[]="ser 0.8.3.8";
38 38
 static char flags[]="NOCR:"
39 39
 #ifdef NOCR
40 40
 "On"
... ...
@@ -53,6 +53,18 @@ static char flags[]="NOCR:"
53 53
 #else
54 54
 "Off"
55 55
 #endif
56
+#ifdef NO_DEBUG
57
+", NO_DEBUG"
58
+#endif
59
+#ifdef NO_LOG
60
+", NO_LOG"
61
+#endif
62
+#ifdef DEBUG
63
+", DEBUG"
64
+#endif
65
+#ifdef OLD_PARSER
66
+", OLD_PARSER"
67
+#endif
56 68
 ;
57 69
 
58 70
 static char help_msg[]= "\
... ...
@@ -33,6 +33,7 @@ char* parse_first_line(char* buffer, unsigned int len, struct msg_start * fl)
33 33
 	char* third;
34 34
 	char* nl;
35 35
 	int offset;
36
+	int l;
36 37
 	char* end;
37 38
 	
38 39
 	/* grammar:
... ...
@@ -49,22 +50,24 @@ char* parse_first_line(char* buffer, unsigned int len, struct msg_start * fl)
49 49
 		LOG(L_INFO, "ERROR:parse_first_line: empty  or bad first line\n");
50 50
 		goto error1;
51 51
 	}
52
-	if ((strlen(SIP_VERSION)==(tmp-buffer)) &&
53
-		(memcmp(buffer,SIP_VERSION,tmp-buffer)==0)){
52
+	l=tmp-buffer;
53
+	if ((SIP_VERSION_LEN==l) &&
54
+		(memcmp(buffer,SIP_VERSION,l)==0)){
54 55
 		
55 56
 		fl->type=SIP_REPLY;
56 57
 	}else{
57 58
 		fl->type=SIP_REQUEST;
58 59
 	}
59 60
 	
60
-	offset=tmp-buffer;
61
+	offset=l;
61 62
 	second=eat_space(tmp, len-offset);
62 63
 	offset+=second-tmp;
63 64
 	if ((second==tmp)||(tmp>=end)){
64 65
 		goto error;
65 66
 	}
66 67
 	*tmp=0; /* mark the end of the token */
67
-	fl->u.request.method=buffer;
68
+	fl->u.request.method.s=buffer;
69
+	fl->u.request.method.len=l;
68 70
 	
69 71
 	/* next element */
70 72
 	tmp=eat_token(second, len-offset);
... ...
@@ -78,7 +81,9 @@ char* parse_first_line(char* buffer, unsigned int len, struct msg_start * fl)
78 78
 		goto error;
79 79
 	}
80 80
 	*tmp=0; /* mark the end of the token */
81
-	fl->u.request.uri=second;
81
+	fl->u.request.uri.s=second;
82
+	fl->u.request.uri.len=tmp-second;
83
+
82 84
 	/*  last part: for a request it must be the version, for a reply
83 85
 	 *  it can contain almost anything, including spaces, so we don't care
84 86
 	 *  about it*/
... ...
@@ -104,7 +109,8 @@ char* parse_first_line(char* buffer, unsigned int len, struct msg_start * fl)
104 104
 		goto error;
105 105
 	}
106 106
 	*tmp=0;
107
-	fl->u.request.version=third;
107
+	fl->u.request.version.s=third;
108
+	fl->u.request.version.len=tmp-third;
108 109
 	
109 110
 	return nl;
110 111
 
... ...
@@ -120,11 +126,10 @@ error1:
120 120
 }
121 121
 
122 122
 
123
+#ifdef OLD_PARSER
123 124
 /* returns integer field name type */
124
-int field_name(char *s)
125
+int field_name(char *s, int l)
125 126
 {
126
-	int l;
127
-	l=strlen(s);
128 127
 	if (l<1) return HDR_OTHER;
129 128
 	else if ((l==1) && ((*s=='v')||(*s=='V')))
130 129
 		return HDR_VIA;
... ...
@@ -134,10 +139,77 @@ int field_name(char *s)
134 134
 		return HDR_TO;*/
135 135
 	return HDR_OTHER;
136 136
 }
137
+#endif
137 138
 
138 139
 
140
+#ifndef OLD_PARSER
141
+/* returns pointer to next header line, and fill hdr_f ;
142
+ * if at end of header returns pointer to the last crlf  (always buf)*/
143
+char* get_hdr_field(char* buf, unsigned int len, struct hdr_field* hdr)
144
+{
145
+	char* end;
146
+	char* tmp;
147
+	char *match;
148
+	struct via_body *vb;
149
+
150
+	end=buf+len;
151
+	if ((*buf)=='\n' || (*buf)=='\r'){
152
+		/* double crlf or lflf or crcr */
153
+		DBG("found end of header\n");
154
+		hdr->type=HDR_EOH;
155
+		return buf;
156
+	}
139 157
 
158
+	tmp=parse_hname(buf, end, hdr);
159
+	if (hdr->type==HDR_ERROR){
160
+		LOG(L_ERR, "ERROR: get_hdr_field: bad header\n");
161
+		goto error;
162
+	}else if (hdr->type==HDR_VIA){
163
+		vb=malloc(sizeof(struct via_body));
164
+		if (vb==0){
165
+			LOG(L_ERR, "get_hdr_field: out of memory\n");
166
+			goto error;
167
+		}
168
+		memset(vb,0,sizeof(struct via_body));
140 169
 
170
+		hdr->body.s=tmp;
171
+		tmp=parse_via(tmp, end, vb);
172
+		if (vb->error==VIA_PARSE_ERROR){
173
+			LOG(L_ERR, "ERROR: get_hdr_field: bad via\n");
174
+			free(vb);
175
+			goto error;
176
+		}
177
+		hdr->parsed=vb;
178
+		vb->hdr.s=hdr->name.s;
179
+		vb->hdr.len=hdr->name.len;
180
+		vb->size=tmp-hdr->name.s;
181
+		hdr->body.len=tmp-hdr->body.s;
182
+	}else{
183
+		/* just skip over it*/
184
+		hdr->body.s=tmp;
185
+		/* find lf*/
186
+		match=memchr(tmp, '\n', end-tmp);
187
+		if (match){
188
+			/* null terminate*/
189
+			*match=0;
190
+			hdr->body.len=match-tmp;
191
+			match++; /*skip*/
192
+			tmp=match;
193
+		}else {
194
+			tmp=end;
195
+			LOG(L_ERR, "ERROR: get_hdr_field: bad body for <%s>(%d)\n",
196
+					hdr->name.s, hdr->type);
197
+			goto error;
198
+		}
199
+	}
200
+	return tmp;
201
+error:
202
+	DBG("get_hdr_field: error exit\n");
203
+	hdr->type=HDR_ERROR;
204
+	return tmp;
205
+}
206
+
207
+#else
141 208
 /* returns pointer to next header line, and fill hdr_f */
142 209
 char* get_hdr_field(char *buffer, unsigned int len, struct hdr_field*  hdr_f)
143 210
 {
... ...
@@ -151,11 +223,14 @@ char* get_hdr_field(char *buffer, unsigned int len, struct hdr_field*  hdr_f)
151 151
 	char* nl;
152 152
 	char* body;
153 153
 	int offset;
154
+	int l;
154 155
 
155 156
 	
156 157
 	/* init content to the empty string */
157
-	hdr_f->name="";
158
-	hdr_f->body="";
158
+	hdr_f->name.s="";
159
+	hdr_f->name.len=0;
160
+	hdr_f->body.s="";
161
+	hdr_f->body.len=0;
159 162
 	
160 163
 	if ((*buffer=='\n')||(*buffer=='\r')){
161 164
 		/* double crlf */
... ...
@@ -175,6 +250,7 @@ char* get_hdr_field(char *buffer, unsigned int len, struct hdr_field*  hdr_f)
175 175
 	tmp2=eat_token(buffer, tmp-buffer);
176 176
 	/* in the worst case tmp2=buffer+tmp-buffer=tmp */
177 177
 	*tmp2=0;
178
+	l=tmp2-buffer;
178 179
 	if (tmp2<tmp){
179 180
 		tmp2++;
180 181
 		/* catch things like: "Via foo bar:" */
... ...
@@ -185,9 +261,10 @@ char* get_hdr_field(char *buffer, unsigned int len, struct hdr_field*  hdr_f)
185 185
 		}
186 186
 	}
187 187
 
188
-	hdr_f->type=field_name(buffer);
188
+	hdr_f->type=field_name(buffer, l);
189 189
 	body= ++tmp;
190
-	hdr_f->name=buffer;
190
+	hdr_f->name.s=buffer;
191
+	hdr_f->name.len=l;
191 192
 	offset=tmp-buffer;
192 193
 	/* get all the lines in this field  body */
193 194
 	do{
... ...
@@ -202,20 +279,22 @@ char* get_hdr_field(char *buffer, unsigned int len, struct hdr_field*  hdr_f)
202 202
 		goto error;
203 203
 	}
204 204
 	*(tmp-1)=0; /* should be an LF */
205
-	hdr_f->body=body;
205
+	hdr_f->body.s=body;
206
+	hdr_f->body.len=tmp-1-body;;
206 207
 error:
207 208
 	return tmp;
208 209
 }
210
+#endif
209 211
 
210 212
 
211
-
212
-char* parse_hostport(char* buf, char** host, short int* port)
213
+char* parse_hostport(char* buf, str* host, short int* port)
213 214
 {
214 215
 	char *tmp;
215 216
 	char *invalid;
216 217
 	
217
-	*host=buf;
218
+	host->s=buf;
218 219
 	for(tmp=buf;(*tmp)&&(*tmp!=':');tmp++);
220
+	host->len=tmp-buf;
219 221
 	if (*tmp==0){
220 222
 		*port=0;
221 223
 	}else{
... ...
@@ -229,7 +308,7 @@ char* parse_hostport(char* buf, char** host, short int* port)
229 229
 			/* report error? */
230 230
 		}
231 231
 	}
232
-	return *host;
232
+	return host->s;
233 233
 }
234 234
 
235 235
 
... ...
@@ -265,8 +344,8 @@ int parse_uri(char *buf, int len, struct sip_uri* uri)
265 265
 	next=memchr(buf,'@', end-buf);
266 266
 	if (next==0){
267 267
 		/* no '@' found, => no userinfo */
268
-		uri->user=0;
269
-		uri->passwd=0;
268
+		uri->user.s=0;
269
+		uri->passwd.s=0;
270 270
 		host=buf;
271 271
 	}else{
272 272
 		/* found it */
... ...
@@ -275,33 +354,37 @@ int parse_uri(char *buf, int len, struct sip_uri* uri)
275 275
 		passwd=memchr(user,':', next-user);
276 276
 		if (passwd==0){
277 277
 			/* no ':' found => no password */
278
-			uri->passwd=0;
279
-			uri->user=(char*)malloc(next-user+1);
280
-			if (uri->user==0){
278
+			uri->passwd.s=0;
279
+			uri->user.s=(char*)malloc(next-user+1);
280
+			if (uri->user.s==0){
281 281
 				LOG(L_ERR,"ERROR:parse_uri: memory allocation failure\n");
282 282
 				ret=E_OUT_OF_MEM;
283 283
 				goto error;
284 284
 			}
285
-			memcpy(uri->user,user, next-user);
286
-			uri->user[next-user]=0; /* null terminate it, usefull for easy printing*/
285
+			memcpy(uri->user.s, user, next-user);
286
+			uri->user.len=next-user;
287
+			uri->user.s[next-user]=0; /* null terminate it, 
288
+									   usefull for easy printing*/
287 289
 		}else{
288
-			uri->user=(char*)malloc(passwd-user+1);
289
-			if (uri->user==0){
290
+			uri->user.s=(char*)malloc(passwd-user+1);
291
+			if (uri->user.s==0){
290 292
 				LOG(L_ERR,"ERROR:parse_uri: memory allocation failure\n");
291 293
 				ret=E_OUT_OF_MEM;
292 294
 				goto error;
293 295
 			}
294
-			memcpy(uri->user,user, passwd-user);
295
-			uri->user[passwd-user]=0;
296
+			memcpy(uri->user.s, user, passwd-user);
297
+			uri->user.len=passwd-user;
298
+			uri->user.s[passwd-user]=0;
296 299
 			passwd++; /*skip ':' */
297
-			uri->passwd=(char*)malloc(next-passwd+1);
298
-			if (uri->passwd==0){
300
+			uri->passwd.s=(char*)malloc(next-passwd+1);
301
+			if (uri->passwd.s==0){
299 302
 				LOG(L_ERR,"ERROR:parse_uri: memory allocation failure\n");
300 303
 				ret=E_OUT_OF_MEM;
301 304
 				goto error;
302 305
 			}
303
-			memcpy(uri->passwd,passwd, next-passwd);
304
-			uri->passwd[next-passwd]=0;
306
+			memcpy(uri->passwd.s, passwd, next-passwd);
307
+			uri->passwd.len=next-passwd;
308
+			uri->passwd.s[next-passwd]=0;
305 309
 		}
306 310
 		host=next+1; /* skip '@' */
307 311
 	}
... ...
@@ -316,14 +399,15 @@ int parse_uri(char *buf, int len, struct sip_uri* uri)
316 316
 	port=memchr(host,':',end-host);
317 317
 	host_len=(port)?port-host:(params)?params-host:(headers)?headers-host:end-host;
318 318
 	/* get host */
319
-	uri->host=malloc(host_len+1);
320
-	if (uri->host==0){
319
+	uri->host.s=malloc(host_len+1);
320
+	if (uri->host.s==0){
321 321
 		LOG(L_ERR, "ERROR: parse_uri: memory allocation error\n");
322 322
 		ret=E_OUT_OF_MEM;
323 323
 		goto error;
324 324
 	}
325
-	memcpy(uri->host,host, host_len);
326
-	uri->host[host_len]=0;
325
+	memcpy(uri->host.s, host, host_len);
326
+	uri->host.len=host_len;
327
+	uri->host.s[host_len]=0;
327 328
 	/* get port*/
328 329
 	if ((port)&&(port+1<end)){
329 330
 		port++;
... ...
@@ -334,15 +418,16 @@ int parse_uri(char *buf, int len, struct sip_uri* uri)
334 334
 			goto error;
335 335
 		}
336 336
 		port_len=(params)?params-port:(headers)?headers-port:end-port;
337
-		uri->port=malloc(port_len+1);
338
-		if (uri->port==0){
337
+		uri->port.s=malloc(port_len+1);
338
+		if (uri->port.s==0){
339 339
 			LOG(L_ERR, "ERROR: parse_uri: memory allocation error\n");
340 340
 			ret=E_OUT_OF_MEM;
341 341
 			goto error;
342 342
 		}
343
-		memcpy(uri->port, port, port_len);
344
-		uri->port[port_len]=0;
345
-	}else uri->port=0;
343
+		memcpy(uri->port.s, port, port_len);
344
+		uri->port.len=port_len;
345
+		uri->port.s[port_len]=0;
346
+	}else uri->port.s=0;
346 347
 	/* get params */
347 348
 	if ((params)&&(params+1<end)){
348 349
 		params++;
... ...
@@ -353,28 +438,30 @@ int parse_uri(char *buf, int len, struct sip_uri* uri)
353 353
 			goto error;
354 354
 		}
355 355
 		params_len=(headers)?headers-params:end-params;
356
-		uri->params=malloc(params_len+1);
357
-		if (uri->params==0){
356
+		uri->params.s=malloc(params_len+1);
357
+		if (uri->params.s==0){
358 358
 			LOG(L_ERR, "ERROR: parse_uri: memory allocation error\n");
359 359
 			ret=E_OUT_OF_MEM;
360 360
 			goto error;
361 361
 		}
362
-		memcpy(uri->params, params, params_len);
363
-		uri->params[params_len]=0;
364
-	}else uri->params=0;
362
+		memcpy(uri->params.s, params, params_len);
363
+		uri->params.len=params_len;
364
+		uri->params.s[params_len]=0;
365
+	}else uri->params.s=0;
365 366
 	/*get headers */
366 367
 	if ((headers)&&(headers+1<end)){
367 368
 		headers++;
368 369
 		headers_len=end-headers;
369
-		uri->headers=malloc(headers_len+1);
370
-		if(uri->headers==0){
370
+		uri->headers.s=malloc(headers_len+1);
371
+		if(uri->headers.s==0){
371 372
 			LOG(L_ERR, "ERROR: parse_uri: memory allocation error\n");
372 373
 			ret=E_OUT_OF_MEM;
373 374
 			goto error;
374 375
 		}
375
-		memcpy(uri->headers, headers, headers_len);
376
-		uri->headers[headers_len]=0;
377
-	}else uri->headers=0;
376
+		memcpy(uri->headers.s, headers, headers_len);
377
+		uri->headers.len=headers_len;
378
+		uri->headers.s[headers_len]=0;
379
+	}else uri->headers.s=0;
378 380
 	
379 381
 	return ret;
380 382
 error:
... ...
@@ -396,13 +483,15 @@ char* parse_via_body(char* buffer,unsigned int len, struct via_body * vb)
396 396
 
397 397
 	char* tmp;
398 398
 	char *name,*version, *transport, *comment, *params, *hostport;
399
+	int name_len, version_len, transport_len, comment_len, params_len;
399 400
 	char * next_via;
400
-	char * host;
401
+	str host;
401 402
 	short int port;
402 403
 	int offset;
403 404
 	
404 405
 
405
-	name=version=transport=comment=params=hostport=next_via=host=0;
406
+	name=version=transport=comment=params=hostport=next_via=host.s=0;
407
+	name_len=version_len=transport_len=comment_len=params_len=host.len=0;
406 408
 	name=eat_space(buffer, len);
407 409
 	if (name-buffer==len) goto error;
408 410
 	offset=name-buffer;
... ...
@@ -411,18 +500,21 @@ char* parse_via_body(char* buffer,unsigned int len, struct via_body * vb)
411 411
 	version=eat_token2(tmp,len-offset,'/');
412 412
 	if (version+1-buffer>=len) goto error;
413 413
 	*version=0;
414
+	name_len=version-name;
414 415
 	version++;
415 416
 	offset+=version-tmp;
416 417
 	
417 418
 	transport=eat_token2(tmp,len-offset,'/');
418 419
 	if (transport+1-buffer>=len) goto error;
419 420
 	*transport=0;
421
+	version_len=transport-version;
420 422
 	transport++;
421 423
 	offset+=transport-tmp;
422 424
 	
423 425
 	tmp=eat_token(transport,len-offset);
424 426
 	if (tmp+1-buffer>=len) goto error;
425 427
 	*tmp=0;
428
+	transport_len=tmp-transport;
426 429
 	tmp++;
427 430
 	offset+=tmp-transport;
428 431
 	
... ...
@@ -445,6 +537,7 @@ char* parse_via_body(char* buffer,unsigned int len, struct via_body * vb)
445 445
 					for(;((tmp-buffer)<len)&&
446 446
 						(*tmp!=',');tmp++);
447 447
 					/* mark end of compact via (also end of comment)*/
448
+					comment_len=tmp-comment;
448 449
 					if (tmp-buffer<len){
449 450
 						*tmp=0;
450 451
 					}else break;
... ...
@@ -463,6 +556,7 @@ char* parse_via_body(char* buffer,unsigned int len, struct via_body * vb)
463 463
 				/* eat till end, first space  or ',' */
464 464
 				for(;((tmp-buffer)<len)&&
465 465
 					(*tmp!=' '&& *tmp!=',');tmp++);
466
+				params_len=tmp-params;
466 467
 				if (tmp-buffer==len)  break;
467 468
 				if (*tmp==' '){
468 469
 					/* eat comment */
... ...
@@ -471,10 +565,12 @@ char* parse_via_body(char* buffer,unsigned int len, struct via_body * vb)
471 471
 					comment=tmp;
472 472
 					for(;((tmp-buffer)<len)&&
473 473
 						(*tmp!=',');tmp++);
474
+					comment_len=tmp-comment;
474 475
 					if (tmp-buffer==len)  break;
475 476
 				}
476 477
 				/* mark end of via*/
477 478
 				*tmp=0;
479
+				
478 480
 				/* eat space & ',' */
479 481
 				for(tmp=tmp+1;((tmp-buffer)<len)&&
480 482
 					(*tmp==' '|| *tmp==',');tmp++);
... ...
@@ -496,13 +592,19 @@ char* parse_via_body(char* buffer,unsigned int len, struct via_body * vb)
496 496
 	
497 497
 	/* parse hostport */
498 498
 	parse_hostport(hostport, &host, &port);
499
-	vb->name=name;
500
-	vb->version=version;
501
-	vb->transport=transport;
502
-	vb->host=host;
499
+	vb->name.s=name;
500
+	vb->name.len=name_len;
501
+	vb->version.s=version;
502
+	vb->version.len=version_len;
503
+	vb->transport.s=transport;
504
+	vb->transport.len=transport_len;
505
+	vb->host.s=host.s;
506
+	vb->host.len=host.len;
503 507
 	vb->port=port;
504
-	vb->params=params;
505
-	vb->comment=comment;
508
+	vb->params.s=params;
509
+	vb->params.len=params_len;
510
+	vb->comment.s=comment;
511
+	vb->comment.len=comment_len;
506 512
 	vb->next=next_via;
507 513
 	vb->error=VIA_PARSE_OK;
508 514
 
... ...
@@ -526,46 +628,49 @@ int parse_msg(char* buf, unsigned int len, struct sip_msg* msg)
526 526
 	char* rest;
527 527
 	char* first_via;
528 528
 	char* second_via;
529
-	struct msg_start fl;
529
+	struct msg_start *fl;
530 530
 	struct hdr_field hf;
531
-	struct via_body vb1, vb2;
531
+	struct via_body *vb1, *vb2;
532 532
 	int offset;
533 533
 
534
-	
534
+#ifdef OLD_PARSER
535 535
 	/* init vb1 & vb2 to the null string */
536
-	vb1.error=VIA_PARSE_ERROR;
537
-	vb1.hdr=vb1.name=vb1.version=vb1.transport=vb1.host=0;
538
-	vb1.params=vb1.comment=0;
539
-	vb1.next=0;
540
-	vb1.size=0;
541
-	memcpy(&vb2, &vb1, sizeof(struct via_body));
542
-
536
+	/*memset(&vb1,0, sizeof(struct via_body));
537
+	memset(&vb2,0, sizeof(struct via_body));*/
538
+	vb1=&(msg->via1);
539
+	vb2=&(msg->via2);
540
+	vb1->error=VIA_PARSE_ERROR;
541
+	vb2->error=VIA_PARSE_ERROR;
542
+#else
543
+	vb1=vb2=0;
544
+#endif
543 545
 	/* eat crlf from the beginning */
544 546
 	for (tmp=buf; (*tmp=='\n' || *tmp=='\r')&&
545 547
 			tmp-buf < len ; tmp++);
546 548
 	offset=tmp-buf;
547
-	rest=parse_first_line(tmp, len-offset, &fl);
549
+	fl=&(msg->first_line);
550
+	rest=parse_first_line(tmp, len-offset, fl);
548 551
 	offset+=rest-tmp;
549 552
 	tmp=rest;
550
-	switch(fl.type){
553
+	switch(fl->type){
551 554
 		case SIP_INVALID:
552 555
 			DBG("parse_msg: invalid message\n");
553 556
 			goto error;
554 557
 			break;
555 558
 		case SIP_REQUEST:
556 559
 			DBG("SIP Request:\n");
557
-			DBG(" method:  <%s>\n",fl.u.request.method);
558
-			DBG(" uri:     <%s>\n",fl.u.request.uri);
559
-			DBG(" version: <%s>\n",fl.u.request.version);
560
+			DBG(" method:  <%s>\n",fl->u.request.method);
561
+			DBG(" uri:     <%s>\n",fl->u.request.uri);
562
+			DBG(" version: <%s>\n",fl->u.request.version);
560 563
 			break;
561 564
 		case SIP_REPLY:
562 565
 			DBG("SIP Reply  (status):\n");
563
-			DBG(" version: <%s>\n",fl.u.reply.version);
564
-			DBG(" status:  <%s>\n",fl.u.reply.status);
565
-			DBG(" reason:  <%s>\n",fl.u.reply.reason);
566
+			DBG(" version: <%s>\n",fl->u.reply.version);
567
+			DBG(" status:  <%s>\n",fl->u.reply.status);
568
+			DBG(" reason:  <%s>\n",fl->u.reply.reason);
566 569
 			break;
567 570
 		default:
568
-			DBG("unknown type %d\n",fl.type);
571
+			DBG("unknown type %d\n",fl->type);
569 572
 	}
570 573
 	
571 574
 	/*find first Via: */
... ...
@@ -583,88 +688,112 @@ int parse_msg(char* buf, unsigned int len, struct sip_msg* msg)
583 583
 				goto skip;
584 584
 			case HDR_VIA:
585 585
 				if (first_via==0){
586
-						first_via=hf.body;
587
-						vb1.hdr=hf.name;
586
+					first_via=hf.body.s;
587
+#ifndef OLD_PARSER
588
+					vb1=(struct via_body*)hf.parsed;
589
+#else
590
+						vb1->hdr.s=hf.name.s;
591
+						vb1->hdr.len=hf.name.len;
588 592
 						/* replace cr/lf with space in first via */
589 593
 						for (bar=first_via;(first_via) && (*bar);bar++)
590 594
 							if ((*bar=='\r')||(*bar=='\n'))	*bar=' ';
595
+#endif
591 596
 				#ifdef DEBUG
592 597
 						DBG("first via: <%s>\n", first_via);
593 598
 				#endif
594
-						bar=parse_via_body(first_via, strlen(first_via), &vb1);
595
-						if (vb1.error!=VIA_PARSE_OK){
599
+#ifdef OLD_PARSER
600
+						bar=parse_via_body(first_via, hf.body.len, vb1);
601
+						if (vb1->error!=VIA_PARSE_OK){
596 602
 							LOG(L_INFO, "ERROR: parsing via body: %s\n",
597 603
 									first_via);
598 604
 							goto error;
599 605
 						}
600 606
 						
601
-						vb1.size=bar-first_via+first_via-vb1.hdr; 
607
+						vb1->size=bar-first_via+first_via-vb1->hdr.s; 
602 608
 						
609
+#endif
603 610
 						/* compact via */
604
-						if (vb1.next) {
605
-							second_via=vb1.next;
611
+						if (vb1->next) {
612
+							second_via=vb1->next;
606 613
 							/* not interested in the rest of the header */
607 614
 							goto skip;
608 615
 						}else{
616
+#ifdef OLD_PARSER
609 617
 						/*  add 1 (we don't see the trailing lf which
610 618
 						 *  was zeroed by get_hfr_field) */
611
-							vb1.size+=1;
619
+							vb1->size+=1;
620
+#endif
612 621
 						}
613
-						if (fl.type!=SIP_REPLY) goto skip; /* we are interested in the 2nd via 
614
-															   only in replies */
622
+						if (fl->type!=SIP_REPLY) goto skip; /* we are interested
623
+															  in the 2nd via 
624
+															 only in replies */
615 625
 				}else if (second_via==0){
616
-							second_via=hf.body;
617
-							vb2.hdr=hf.name;
626
+							second_via=hf.body.s;
627
+#ifndef OLD_PARSER
628
+							vb2=hf.parsed;
629
+#else
630
+							vb2->hdr.s=hf.name.s;
631
+							vb2->hdr.len=hf.name.len;
632
+#endif
618 633
 							goto skip;
619 634
 				}
620 635
 				break;
621 636
 		}
622 637
 	#ifdef DEBUG
623 638
 		DBG("header field type %d, name=<%s>, body=<%s>\n",
624
-			hf.type, hf.name, hf.body);
639
+			hf.type, hf.name.s, hf.body.s);
625 640
 	#endif
626 641
 		tmp=rest;
627 642
 	}while(hf.type!=HDR_EOH && rest-buf < len);
628 643
 
629 644
 skip:
630 645
 	/* replace cr/lf with space in the second via */
646
+#ifdef OLD_PARSER
631 647
 	for (tmp=second_via;(second_via) && (*tmp);tmp++)
632 648
 		if ((*tmp=='\r')||(*tmp=='\n'))	*tmp=' ';
633 649
 
634 650
 	if (second_via) {
635
-		tmp=parse_via_body(second_via, strlen(second_via), &vb2);
636
-		if (vb2.error!=VIA_PARSE_OK){
651
+		tmp=parse_via_body(second_via, hf.body.len, vb2);
652
+		if (vb2->error!=VIA_PARSE_OK){
637 653
 			LOG(L_INFO, "ERROR: parsing via2 body: %s\n", second_via);
638 654
 			goto error;
639 655
 		}
640
-		vb2.size=tmp-second_via; 
641
-		if (vb2.next==0) vb2.size+=1; /* +1 from trailing lf */
642
-		if (vb2.hdr) vb2.size+=second_via-vb2.hdr;
656
+		vb2->size=tmp-second_via; 
657
+		if (vb2->next==0) vb2->size+=1; /* +1 from trailing lf */
658
+		if (vb2->hdr.s) vb2->size+=second_via-vb2->hdr.s;
643 659
 	}
660
+#endif
644 661
 	
645 662
 
646 663
 #ifdef DEBUG
647 664
 	/* dump parsed data */
648 665
 	if (first_via){
649
-		DBG(" first  via: <%s/%s/%s> <%s:%d>",
650
-				vb1.name, vb1.version, vb1.transport, vb1.host, vb1.port);
651
-		if (vb1.params)  DBG(";<%s>", vb1.params);
652
-		if (vb1.comment) DBG(" <%s>", vb1.comment);
666
+		DBG(" first  via: <%s/%s/%s> <%s:%s(%d)>",
667
+				vb1->name.s, vb1->version.s, vb1->transport.s, vb1->host.s,
668
+				vb1->port_str, vb1->port);
669
+		if (vb1->params.s)  DBG(";<%s>", vb1->params.s);
670
+		if (vb1->comment.s) DBG(" <%s>", vb1->comment.s);
653 671
 		DBG ("\n");
654 672
 	}
673
+#ifdef OLD_PARSER
655 674
 	if (second_via){
656 675
 		DBG(" second via: <%s/%s/%s> <%s:%d>",
657
-				vb2.name, vb2.version, vb2.transport, vb2.host, vb2.port);
658
-		if (vb2.params)  DBG(";<%s>", vb2.params);
659
-		if (vb2.comment) DBG(" <%s>", vb2.comment);
676
+				vb2->name.s, vb2->version.s, vb2->transport.s, vb2->host.s,
677
+				vb2->port);
678
+		if (vb2->params.s)  DBG(";<%s>", vb2->params.s);
679
+		if (vb2->comment.s) DBG(" <%s>", vb2->comment.s);
660 680
 		DBG("\n");
661 681
 	}
662 682
 #endif
683
+#endif
663 684
 	
664 685
 	/* copy data into msg */
665
-	memcpy(&(msg->first_line), &fl, sizeof(struct msg_start));
666
-	memcpy(&(msg->via1), &vb1, sizeof(struct via_body));
667
-	memcpy(&(msg->via2), &vb2, sizeof(struct via_body));
686
+#ifndef OLD_PARSER
687
+	memcpy(&(msg->via1), vb1, sizeof(struct via_body));
688
+	if (second_via) memcpy(&(msg->via2), vb2, sizeof(struct via_body));
689
+	if (vb1) free(vb1);
690
+	if (vb2) free(vb1);
691
+#endif
668 692
 
669 693
 #ifdef DEBUG
670 694
 	DBG("exiting parse_msg\n");
... ...
@@ -673,6 +802,10 @@ skip:
673 673
 	return 0;
674 674
 	
675 675
 error:
676
+#ifndef OLD_PARSER
677
+	if (vb1) free(vb1);
678
+	if (vb2) free(vb1);
679
+#endif
676 680
 	return -1;
677 681
 }
678 682
 
... ...
@@ -681,11 +814,11 @@ error:
681 681
 void free_uri(struct sip_uri* u)
682 682
 {
683 683
 	if (u){
684
-		if (u->user) free(u->user);
685
-		if (u->passwd) free(u->passwd);
686
-		if (u->host) free(u->host);
687
-		if (u->port) free(u->port);
688
-		if (u->params) free(u->params);
689
-		if (u->headers) free(u->headers);
684
+		if (u->user.s) free(u->user.s);
685
+		if (u->passwd.s) free(u->passwd.s);
686
+		if (u->host.s) free(u->host.s);
687
+		if (u->port.s) free(u->port.s);
688
+		if (u->params.s) free(u->params.s);
689
+		if (u->headers.s) free(u->headers.s);
690 690
 	}
691 691
 }
... ...
@@ -5,7 +5,7 @@
5 5
 #ifndef msg_parser_h
6 6
 #define msg_parser_h
7 7
 
8
-
8
+#include "str.h"
9 9
 #include "data_lump.h"
10 10
 
11 11
 #define SIP_REQUEST 1
... ...
@@ -13,51 +13,55 @@
13 13
 #define SIP_INVALID 0
14 14
 
15 15
 
16
-#define HDR_ERROR 0
17
-/* end of header */
18
-#define HDR_EOH   -1
19
-#define HDR_OTHER 1
20
-#define HDR_VIA   2
21
-#define HDR_TO    3
16
+enum {	HDR_EOH=-1, HDR_ERROR=0, HDR_OTHER,
17
+		HDR_VIA, HDR_TO, HDR_FROM, HDR_CSEQ, HDR_CALLID, HDR_CONTACT,
18
+		HDR_MAXFORWARDS, HDR_ROUTE
19
+	};
20
+
21
+
22
+
22 23
 
23 24
 #define VIA_PARSE_OK	1
24 25
 #define VIA_PARSE_ERROR -1
25 26
 
26 27
 #define SIP_VERSION	"SIP/2.0"
28
+#define SIP_VERSION_LEN 7
27 29
 
28 30
 
29 31
 struct msg_start{
30 32
 	int type;
31 33
 	union {
32 34
 		struct {
33
-			char* method;
34
-			char* uri;
35
-			char* version;
35
+			str method;
36
+			str uri;
37
+			str version;
36 38
 		}request;
37 39
 		struct {
38
-			char* version;
39
-			char* status;
40
-			char* reason;
40
+			str version;
41
+			str status;
42
+			str reason;
41 43
 		}reply;
42 44
 	}u;
43 45
 };
44 46
 
45 47
 struct hdr_field{   /* format: name':' body */
46 48
 	int type;
47
-	char* name;
48
-	char* body;
49
+	str name;
50
+	str body;
51
+	void* parsed;
49 52
 };
50 53
 
51 54
 struct via_body{  /* format: name/version/transport host:port;params comment */
52 55
 	int error;
53
-	char *hdr;   /* contains "Via" or "v" */
54
-	char* name;
55
-	char* version;
56
-	char* transport;
57
-	char* host;
56
+	str hdr;   /* contains "Via" or "v" */
57
+	str name;
58
+	str version;
59
+	str transport;
60
+	str host;
58 61
 	int port;
59
-	char* params;
60
-	char* comment;
62
+	str port_str;
63
+	str params;
64
+	str comment;
61 65
 	int size;    /* full size, including hdr */
62 66
 	char* next; /* pointer to next via body string if compact via or null */
63 67
 };
... ...
@@ -76,7 +80,7 @@ struct sip_msg{
76 76
 	unsigned int len; /* message len (orig) */
77 77
 
78 78
 	/* modifications */
79
-	char* new_uri; /* changed first line uri*/
79
+	str new_uri; /* changed first line uri*/
80 80
 
81 81
 	struct lump* add_rm;      /* used for all the forwarded messages */
82 82
 	struct lump* repl_add_rm; /* only for localy generated replies !!!*/
... ...
@@ -85,25 +89,35 @@ struct sip_msg{
85 85
 
86 86
 
87 87
 struct sip_uri{
88
-	char* user;
89
-	char* passwd;
90
-	char* host;
91
-	char* port;
92
-	char* params;
93
-	char* headers;
88
+	str user;
89
+	str passwd;
90
+	str host;
91
+	str port;
92
+	str params;
93
+	str headers;
94 94
 };
95 95
 
96 96
 
97 97
 
98 98
 char* parse_first_line(char* buffer, unsigned int len, struct msg_start * fl);
99
+#ifdef OLD_PARSER 
99 100
 char* get_hdr_field(char *buffer, unsigned int len, struct hdr_field*  hdr_f);
100
-int field_name(char *s);
101
-char* parse_hostport(char* buf, char** host, short int* port);
101
+int field_name(char *s, int len);
102
+#endif
103
+char* parse_hostport(char* buf, str* host, short int* port);
104
+
105
+#ifdef OLD_PARSER 
102 106
 char* parse_via_body(char* buffer,unsigned int len, struct via_body * vb);
107
+#endif
103 108
 int parse_msg(char* buf, unsigned int len, struct sip_msg* msg);
104 109
 int parse_uri(char *buf, int len, struct sip_uri* uri);
105 110
 void free_uri(struct sip_uri* u);
106 111
 
107 112
 
113
+#ifndef OLD_PARSER
114
+char* parse_hname(char* buf, char* end, struct hdr_field* hdr);
115
+char* parse_via(char* buffer, char* end, struct via_body *vb);
116
+#endif
117
+
108 118
 
109 119
 #endif
... ...
@@ -46,16 +46,19 @@ int receive_msg(char* buf, unsigned int len, unsigned long src_ip)
46 46
 	if (parse_msg(buf,len, &msg)!=0){
47 47
 		goto error;
48 48
 	}
49
+	DBG("Ater parse_msg...\n");
49 50
 	
50 51
 	if (msg.first_line.type==SIP_REQUEST){
52
+		DBG("msg= request\n");
51 53
 		/* sanity checks */
52 54
 		if (msg.via1.error!=VIA_PARSE_OK){
53 55
 			/* no via, send back error ? */
54 56
 			goto skip;
55 57
 		}
56
-		/* check if neccesarry to add receive? */
58
+		/* check if neccesarry to add receive?->moved to forward_req */
57 59
 		
58 60
 		/* exec routing script */
61
+		DBG("preparing to run routing scripts...\n");
59 62
 		if (run_actions(rlist[0], &msg)<0){
60 63
 			LOG(L_WARN, "WARNING: receive_msg: "
61 64
 					"error while trying script\n");
... ...
@@ -66,6 +69,7 @@ int receive_msg(char* buf, unsigned int len, unsigned long src_ip)
66 66
 		else stats.ok_rx_rq++;	
67 67
 #endif
68 68
 	}else if (msg.first_line.type==SIP_REPLY){
69
+		DBG("msg= reply\n");
69 70
 		/* sanity checks */
70 71
 		if (msg.via1.error!=VIA_PARSE_OK){
71 72
 			/* no via, send back error ? */
... ...
@@ -90,13 +94,13 @@ int receive_msg(char* buf, unsigned int len, unsigned long src_ip)
90 90
 		}
91 91
 	}
92 92
 skip:
93
-	if (msg.new_uri) free(msg.new_uri);
93
+	if (msg.new_uri.s) { free(msg.new_uri.s); msg.new_uri.len=0; }
94 94
 	if (msg.add_rm) free_lump_list(msg.add_rm);
95 95
 	if (msg.repl_add_rm) free_lump_list(msg.repl_add_rm);
96 96
 	free(msg.orig);
97 97
 	return 0;
98 98
 error:
99
-	if (msg.new_uri) free(msg.new_uri);
99
+	if (msg.new_uri.s) free(msg.new_uri.s);
100 100
 	if (msg.add_rm) free_lump_list(msg.add_rm);
101 101
 	if (msg.repl_add_rm) free_lump_list(msg.repl_add_rm);
102 102
 	free(msg.orig);
... ...
@@ -292,15 +292,15 @@ static int eval_elem(struct expr* e, struct sip_msg* msg)
292 292
 	}
293 293
 	switch(e->l.operand){
294 294
 		case METHOD_O:
295
-				ret=comp_str(msg->first_line.u.request.method, e->r.param,
295
+				ret=comp_str(msg->first_line.u.request.method.s, e->r.param,
296 296
 								e->op, e->subtype);
297 297
 				break;
298 298
 		case URI_O:
299
-				if(msg->new_uri){
300
-					ret=comp_str(msg->new_uri, e->r.param,
299
+				if(msg->new_uri.s){
300
+					ret=comp_str(msg->new_uri.s, e->r.param,
301 301
 									e->op, e->subtype);
302 302
 				}else{
303
-					ret=comp_str(msg->first_line.u.request.uri, e->r.param,
303
+					ret=comp_str(msg->first_line.u.request.uri.s, e->r.param,
304 304
 									e->op, e->subtype);
305 305
 				}
306 306
 				break;
307 307
new file mode 100644
... ...
@@ -0,0 +1,10 @@
0
+SIP/2.0 200 Ok
1
+Via: SIP/2.0/UDP 127.0.0.1
2
+Via: SIP/2.0/UDP 127.0.0.1:5061
3
+From: <sip:x@foo.bar>
4
+To: <sip:y@bar.foo>
5
+Call-ID:9437892232b@foo.bar
6
+CSeq: 143 INVITE
7
+Contact: <sip:x@foo.bar>
8
+
9
+
0 10
new file mode 100644
... ...
@@ -0,0 +1,10 @@
0
+INVITE sip:foo@127.0.0.1:5061 SIP/2.0
1
+Via: SIP/2.0/UDP 127.0.0.1
2
+Via: SIP/2.0/UDP 127.0.0.1:5061
3
+From: <sip:x@foo.bar>
4
+To: <sip:y@bar.foo>
5
+Call-ID:9437892232b@foo.bar
6
+CSeq: 143 INVITE
7
+Contact: <sip:x@foo.bar>
8
+
9
+
0 10
new file mode 100644
... ...
@@ -0,0 +1,127 @@
0
+/* $Id$ */
1
+
2
+
3
+#include <stdio.h>
4
+#include <stdlib.h>
5
+#include <errno.h>
6
+#include <string.h>
7
+#include <ctype.h>
8
+#include <netdb.h>
9
+#include <unistd.h>
10
+#include <sys/types.h>
11
+#include <fcntl.h>
12
+#include <sys/socket.h>
13
+#include <netinet/in.h>
14
+#include <arpa/inet.h>
15
+
16
+
17
+static char *id="$Id$";
18
+static char *version="resolver_test 0.1";
19
+static char* help_msg="\
20
+Usage: resolver -n address [-c count] [-v]\n\
21
+Options:\n\
22
+    -n address    address to be resolved\n\
23
+    -c count      how many times to resolve it\n\
24
+    -v            increase verbosity level\n\
25
+    -V            version number\n\
26
+    -h            this help message\n\
27
+";
28
+
29
+
30
+
31
+int main (int argc, char** argv)
32
+{
33
+	char c;
34
+	struct hostent* he;
35
+	int ok;
36
+	int errors;
37
+	int r;
38
+	char *tmp;
39
+	
40
+	int count;
41
+	int verbose;
42
+	char *address;
43
+	
44
+	/* init */
45
+	count=0;
46
+	verbose=0;
47
+	address=0;
48
+
49
+	ok=errors=0;
50
+
51
+	opterr=0;
52
+	while ((c=getopt(argc,argv, "n:c:vhV"))!=-1){
53
+		switch(c){
54
+			case 'n':
55
+				address=optarg;
56
+				break;
57
+			case 'v':
58
+				verbose++;
59
+				break;
60
+			case 'c':
61
+				count=strtol(optarg, &tmp, 10);
62
+				if ((tmp==0)||(*tmp)){
63
+					fprintf(stderr, "bad count: -c %s\n", optarg);
64
+					goto error;
65
+				}
66
+				break;
67
+			case 'V':
68
+				printf("version: %s\n", version);
69
+				printf("%s\n",id);
70
+				exit(0);
71
+				break;
72
+			case 'h':
73
+				printf("version: %s\n", version);
74
+				printf("%s", help_msg);
75
+				exit(0);
76
+				break;
77
+			case '?':
78
+				if (isprint(optopt))
79
+					fprintf(stderr, "Unknown option `-%c�\n", optopt);
80
+				else
81
+					fprintf(stderr, "Unknown character `\\x%x�\n", optopt);
82
+				goto error;
83
+			case ':':
84
+				fprintf(stderr, "Option `-%c� requires an argument.\n",
85
+						optopt);
86
+				goto error;
87
+				break;
88
+			default:
89
+					abort();
90
+		}
91
+	}
92
+	
93
+	/* check if all the required params are present */
94
+	if (address==0){
95
+		fprintf(stderr, "Missing -a address\n");
96
+		exit(-1);
97
+	}
98
+	if(count==0){
99
+		fprintf(stderr, "Missing count (-c number)\n");
100
+		exit(-1);
101
+	}else if(count<0){
102
+		fprintf(stderr, "Invalid count (-c %d)\n", count);
103
+		exit(-1);
104
+	}
105
+	
106
+
107
+
108
+	/* flood loop */
109
+	for (r=0; r<count; r++){
110
+		if ((verbose>1)&&(r%1000))  putchar('.');
111
+		/* resolve destination loop */
112
+		he=gethostbyname(address);
113
+		if (he==0){
114
+			errors++;
115
+			if (verbose>1) 
116
+				putchar('?');
117
+		}else ok++;
118
+	}
119
+
120
+	printf("\n%d requests, %d succeeded, %d errors\n", count, ok, errors);
121
+
122
+	exit(0);
123
+
124
+error:
125
+	exit(-1);
126
+}