Browse code

lost: use of parser functions parse_pai_header() and parse_from_header() related to #2723

- lost_get_pai_header(): parsing method update
lost_get_from_header(): parsing method update

- memset(): fixed number of bytes to be filled (unrelated)

(cherry picked from commit 791672393aeba320fd1608746fb61e9a96e19d06)

Wolfgang Kampichler authored on 05/05/2021 16:40:05
Showing 1 changed files
... ...
@@ -129,7 +129,7 @@ p_lost_loc_t lost_new_loc(str rurn)
129 129
 		goto err;
130 130
 	}
131 131
 
132
-	memset(urn, 0, rurn.len + 1);
132
+	memset(urn, 0, rurn.len);
133 133
 	memcpy(urn, rurn.s, rurn.len);
134 134
 	urn[rurn.len] = '\0';
135 135
 
... ...
@@ -182,11 +182,11 @@ p_lost_held_t lost_new_held(str s_uri, str s_type, int time, int exact)
182 182
 		goto err;
183 183
 	}
184 184
 
185
-	memset(uri, 0, s_uri.len + 1);
185
+	memset(uri, 0, s_uri.len);
186 186
 	memcpy(uri, s_uri.s, s_uri.len);
187 187
 	uri[s_uri.len] = '\0';
188 188
 
189
-	memset(type, 0, s_type.len + 1);
189
+	memset(type, 0, s_type.len);
190 190
 	memcpy(type, s_type.s, s_type.len);
191 191
 	type[s_type.len] = '\0';
192 192
 
... ...
@@ -280,7 +280,7 @@ char *lost_copy_string(str src, int *lgth)
280 280
 		if(res == NULL) {
281 281
 			PKG_MEM_ERROR;
282 282
 		} else {
283
-			memset(res, 0, src.len + 1);
283
+			memset(res, 0, src.len);
284 284
 			memcpy(res, src.s, src.len);
285 285
 			res[src.len] = '\0';
286 286
 			*lgth = (int)strlen(res);
... ...
@@ -343,7 +343,7 @@ char *lost_get_content(xmlNodePtr node, const char *name, int *lgth)
343 343
 			xmlFree(content);
344 344
 			return cnt;
345 345
 		}
346
-		memset(cnt, 0, len + 1);
346
+		memset(cnt, 0, len);
347 347
 		memcpy(cnt, trimmed, len);
348 348
 		cnt[len] = '\0';
349 349
 	}
... ...
@@ -379,7 +379,7 @@ char *lost_get_property(xmlNodePtr node, const char *name, int *lgth)
379 379
 			xmlFree(content);
380 380
 			return cnt;
381 381
 		}
382
-		memset(cnt, 0, len + 1);
382
+		memset(cnt, 0, len);
383 383
 		memcpy(cnt, content, len);
384 384
 		cnt[len] = '\0';
385 385
 	}
... ...
@@ -423,7 +423,7 @@ char *lost_get_childname(xmlNodePtr node, const char *name, int *lgth)
423 423
 		PKG_MEM_ERROR;
424 424
 		return cnt;
425 425
 	}
426
-	memset(cnt, 0, len + 1);
426
+	memset(cnt, 0, len);
427 427
 	memcpy(cnt, trimmed, len);
428 428
 	cnt[len] = '\0';
429 429
 
... ...
@@ -476,62 +476,51 @@ p_lost_geolist_t lost_get_geolocation_header(struct sip_msg *msg, int *items)
476 476
  */
477 477
 char *lost_get_pai_header(struct sip_msg *msg, int *lgth)
478 478
 {
479
-	struct hdr_field *hf;
480
-	to_body_t *pai_body;
479
+	p_id_body_t *p_body;
480
+
481
+	char *tmp = NULL;
481 482
 	char *res = NULL;
483
+	int len = 0;
482 484
 
483 485
 	*lgth = 0;
484 486
 
485
-	if(parse_headers(msg, HDR_EOH_F, 0) == -1) {
486
-		LM_ERR("failed to parse SIP headers\n");
487
+	if(parse_pai_header(msg) == -1) {
488
+		
489
+		LM_DBG("failed to parse P-A-I header\n");
490
+		
487 491
 		return res;
488 492
 	}
489 493
 
490
-	for(hf = msg->headers; hf; hf = hf->next) {
491
-		if((hf->type == HDR_PAI_T)
492
-				&& (hf->name.len == LOST_PAI_HEADER_SIZE - 2)) {
493
-			/* possible hit */
494
-			if(strncasecmp(hf->name.s, LOST_PAI_HEADER,
495
-								LOST_PAI_HEADER_SIZE) == 0) {
494
+	if(msg->pai == NULL || get_pai(msg) == NULL) {
495
+		LM_ERR("P-A-I header not found\n");
496
+		return res;
497
+	}
498
+	p_body = get_pai(msg);
496 499
 
497
-				LM_DBG("P-A-I body:  [%.*s]\n", hf->body.len, hf->body.s);
500
+	/* warning in case multiple P-A-I headers were found and use first */
501
+	if(p_body->num_ids > 1) {
502
+		LM_WARN("multiple P-A-I headers found, selecting first!\n");
503
+	}
498 504
 
499
-				/* first, get some memory */
500
-				pai_body = pkg_malloc(sizeof(to_body_t));
501
-				if(pai_body == NULL) {
502
-					PKG_MEM_ERROR;
503
-					return res;
504
-				}
505
-				/* parse P-A-I body */
506
-				memset(pai_body, 0, sizeof(to_body_t));
507
-				parse_to(hf->body.s, hf->body.s + hf->body.len + 1, pai_body);
508
-				if(pai_body->error == PARSE_ERROR) {
509
-					LM_ERR("bad P-A-I header\n");
510
-					pkg_free(pai_body);
511
-					return res;
512
-				}
513
-				if(pai_body->error == PARSE_OK) {
514
-					res = (char *)pkg_malloc(
515
-							(pai_body->uri.len + 1) * sizeof(char));
516
-					if(res == NULL) {
517
-						PKG_MEM_ERROR;
518
-						pkg_free(pai_body);
519
-						return res;
520
-					} else {
521
-						memset(res, 0, pai_body->uri.len + 1);
522
-						memcpy(res, pai_body->uri.s, pai_body->uri.len + 1);
523
-						res[pai_body->uri.len] = '\0';
524
-						pkg_free(pai_body);
505
+	LM_DBG("P-A-I body: [%.*s]\n", p_body->id->body.len, p_body->id->body.s);
525 506
 
526
-						*lgth = strlen(res);
527
-					}
528
-				}
529
-			} else {
530
-				LM_ERR("header '%.*s' length %d\n", hf->body.len, hf->body.s,
531
-						hf->body.len);
532
-			}
533
-			break;
534
-		}
507
+	/* accept any identity string (body), but remove <..> if present */ 
508
+	tmp = p_body->id->body.s;
509
+	len = p_body->id->body.len;
510
+	if(tmp[0] == '<' && tmp[len - 1] == '>') {
511
+		tmp++;
512
+		len -= 2;
513
+	}
514
+	/* allocate memory, copy and \0 terminate string */
515
+	res = (char *)pkg_malloc((len + 1) * sizeof(char));
516
+	if(res == NULL) {
517
+		PKG_MEM_ERROR;
518
+		return res;
519
+	} else {
520
+		memset(res, 0, len);
521
+		memcpy(res, tmp, len);
522
+		res[len] = '\0';
523
+		*lgth = strlen(res);
535 524
 	}
536 525
 
537 526
 	return res;
... ...
@@ -641,53 +630,35 @@ int lost_get_nameinfo(char *ip, str *name, int flag)
641 630
  */
642 631
 char *lost_get_from_header(struct sip_msg *msg, int *lgth)
643 632
 {
644
-	struct hdr_field *hf;
645
-	to_body_t *from_body;
633
+	to_body_t *f_body;
646 634
 	char *res = NULL;
647 635
 
648 636
 	*lgth = 0;
649 637
 
650
-	if(parse_headers(msg, HDR_EOH_F, 0) == -1) {
651
-		LM_ERR("failed to parse SIP headers\n");
638
+	if(parse_from_header(msg) == -1) {
639
+		LM_ERR("failed to parse From header\n");
652 640
 		return res;
653 641
 	}
654 642
 
655
-	for(hf = msg->headers; hf; hf = hf->next) {
656
-		if(hf->type == HDR_FROM_T) {
643
+	if(msg->from == NULL || get_from(msg) == NULL) {
644
+		LM_ERR("From header not found\n");
645
+		return res;
646
+	}
647
+	f_body = get_from(msg);
657 648
 
658
-			LM_DBG("From body:  [%.*s]\n", hf->body.len, hf->body.s);
649
+	LM_DBG("From body: [%.*s]\n", f_body->body.len, f_body->body.s);
659 650
 
660
-			/* first, get some memory */
661
-			from_body = pkg_malloc(sizeof(to_body_t));
662
-			if(from_body == NULL) {
663
-				PKG_MEM_ERROR;
664
-				return res;
665
-			}
666
-			/* parse From body */
667
-			memset(from_body, 0, sizeof(to_body_t));
668
-			parse_to(hf->body.s, hf->body.s + hf->body.len + 1, from_body);
669
-			if(from_body->error == PARSE_ERROR) {
670
-				LM_ERR("bad From header\n");
671
-				pkg_free(from_body);
672
-				return res;
673
-			}
674
-			if(from_body->error == PARSE_OK) {
675
-				res = (char *)pkg_malloc(
676
-						(from_body->uri.len + 1) * sizeof(char));
677
-				if(res == NULL) {
678
-					PKG_MEM_ERROR;
679
-					pkg_free(from_body);
680
-					return res;
681
-				} else {
682
-					memset(res, 0, from_body->uri.len + 1);
683
-					memcpy(res, from_body->uri.s, from_body->uri.len + 1);
684
-					res[from_body->uri.len] = '\0';
685
-					pkg_free(from_body);
651
+	/* allocate memory, copy and \0 terminate string */
652
+	res = (char *)pkg_malloc((f_body->uri.len + 1) * sizeof(char));
653
+	if(res == NULL) {
654
+		PKG_MEM_ERROR;
655
+		return res;
656
+	} else {
657
+		memset(res, 0, f_body->uri.len);
658
+		memcpy(res, f_body->uri.s, f_body->uri.len);
659
+		res[f_body->uri.len] = '\0';
686 660
 
687
-					*lgth = strlen(res);
688
-				}
689
-			}
690
-		}
661
+		*lgth = strlen(res);
691 662
 	}
692 663
 
693 664
 	return res;
... ...
@@ -784,8 +755,8 @@ char *lost_copy_geoheader_value(char *src, int len)
784 755
 		PKG_MEM_ERROR;
785 756
 		return res;
786 757
 	} else {
787
-		memset(res, 0, len + 1);
788
-		memcpy(res, src, len + 1);
758
+		memset(res, 0, len);
759
+		memcpy(res, src, len);
789 760
 		res[len] = '\0';
790 761
 	}
791 762
 
... ...
@@ -892,8 +863,8 @@ int lost_new_geoheader_list(p_lost_geolist_t *list, str hdr)
892 863
 
893 864
 							new->type = HTTP;
894 865
 						} else if(((*(search + 5) == 's')
895
-										  || (*(search + 5) == 'S'))
896
-								  && (*(search + 6) == ':')) {
866
+								|| (*(search + 5) == 'S'))
867
+								&& (*(search + 6) == ':')) {
897 868
 
898 869
 							LM_DBG("adding https url [%s]\n", new->value);
899 870
 
... ...
@@ -1397,7 +1368,7 @@ https://tools.ietf.org/html/rfc6753
1397 1368
 		return doc;
1398 1369
 	}
1399 1370
 
1400
-	memset(doc, 0, buffersize + 1);
1371
+	memset(doc, 0, buffersize);
1401 1372
 	memcpy(doc, (char *)xmlbuff, buffersize);
1402 1373
 	doc[buffersize] = '\0';
1403 1374
 
... ...
@@ -1508,7 +1479,7 @@ https://tools.ietf.org/html/rfc5985
1508 1479
 		return doc;
1509 1480
 	}
1510 1481
 
1511
-	memset(doc, 0, buffersize + 1);
1482
+	memset(doc, 0, buffersize);
1512 1483
 	memcpy(doc, (char *)xmlbuff, buffersize);
1513 1484
 	doc[buffersize] = '\0';
1514 1485
 
... ...
@@ -1668,7 +1639,7 @@ https://tools.ietf.org/html/rfc5222
1668 1639
 		return doc;
1669 1640
 	}
1670 1641
 
1671
-	memset(doc, 0, buffersize + 1);
1642
+	memset(doc, 0, buffersize);
1672 1643
 	memcpy(doc, (char *)xmlbuff, buffersize);
1673 1644
 	doc[buffersize] = '\0';
1674 1645